Documentation Index
Fetch the complete documentation index at: https://mintlify.com/cloudflare/partykit/llms.txt
Use this file to discover all available pages before exploring further.
y-partyserver is an addon library for partyserver that hosts backends for Yjs, a high-performance library of data structures for building collaborative software. It exposes a YServer class (a Durable Object) for the server side, a YProvider class for the client side, and a useYProvider React hook — all wired together over WebSocket for conflict-free real-time sync.
This page assumes some familiarity with Yjs. If you’re new to it, start with
the official Yjs documentation.
Installation
Class: YServer
YServer extends PartyServer’s Server class (which itself extends DurableObject). The simplest possible Yjs backend re-exports YServer directly:
YServer:
Property: document
The live Yjs document instance for this room. Apply updates to it inside
onLoad, and read from it inside onSave.Static: callbackOptions
Controls how frequently onSave is invoked after edits.
Milliseconds to wait after the last edit before triggering
onSave.Maximum milliseconds to wait before forcing
onSave, regardless of continued edits.Milliseconds before the
onSave invocation times out.onLoad(): Promise<void>
Called once when the first client connects to a room. Use it to load persisted state into this.document.
onSave(): Promise<void>
Called periodically after edits (debounced via callbackOptions) and when the room empties. Serialize the document with Y.encodeStateAsUpdate and write it to durable storage.
onCustomMessage(connection, message)
Override this method to handle custom string messages sent by clients over the same WebSocket connection used for Yjs sync.
The connection that sent the message.
The raw string message. Use
JSON.parse for structured data.sendCustomMessage(connection, message)
Send a custom string message to a single connection.
The target connection.
The string message to send.
broadcastCustomMessage(message, excludeConnection?)
Broadcast a custom string message to all connections in the room, with an optional exclusion.
The string message to broadcast.
An optional connection to exclude from the broadcast.
Class: YProvider
Import fromy-partyserver/provider. Connects a client-side Yjs document to a YServer room over WebSocket.
Constructor
The hostname (and optional port) of the PartyServer, e.g.
"localhost:8787".The document/room name. Corresponds to the Durable Object instance name.
The Yjs document instance to synchronize.
provider.sendMessage(message)
Send a custom string message to the server. The server receives it via onCustomMessage.
Event: "custom-message"
Listen for custom string messages sent from the server via sendCustomMessage or broadcastCustomMessage.
Hook: useYProvider
Import fromy-partyserver/react. Returns a stable YProvider instance tied to a React component’s lifecycle.
Signature
The server hostname. Defaults to
window.location.host.The document/room name.
The party path segment.
An existing Yjs document instance. If omitted, a new one is created internally.
Any additional
YProvider constructor options (see above).YProvider instance.
Persistence Example
CombineonLoad and onSave for full persistence across sessions: