Y-PartyKit (Yjs API)
y-partykit
is an addon library for partykit
designed to host backends for Yjs, a high-performance library of data structures for building collaborative software.
Setting up a Yjs Server
Using y-partykit
simplifies setting up a Yjs backend. Only a few lines of code are needed:
See Server Configuration for configuration options.
Connecting from the client
Use the provider to connect to this server from your client:
You can add additional options to the provider:
Usage with React
If youβre using React, then you can use the hook version of the provider: useYProvider
.
Server Configuration
For more complex backends, you can pass additional options.
Persistence
By default, PartyKit maintains a copy of the Yjs document as long as at least one client is connected to the server. When all clients disconnect, the document state may be lost.
To persists the Yjs document state between sessions, you can use the built-in PartyKit storage by enabling the persist
option.
y-partykit
supports two modes of persistence: snapshot, and history.
Persisting snapshots (recommended)
In snapshot
mode, PartyKit stores the latest document state between sessions.
During a session, PartyKit accumulates individual updates and stores them as separate records. When an editing session ends due to last connection disconnecting, PartyKit merges all updates to a single snapshot.
The snapshot
mode is optimal for most applications that do not need to support long-lived offline editing sessions.
Persisting update history (advanced)
In history
mode, PartyKit stores the full edit history of the document.
This is useful when multiple clients are expected to be able to change the document while offline, and synchronise their changes later.
For long-lived documents, the edit history would grow indefinitely, eventually reaching the practical limits of a single PartyKit server instance.
To prevent unbounded growth, PartyKit applies a 10MB maximum limit to the edit history. You can customise these limits as follows:
Once either limit is reached, the document is snapshotted, and history tracking is started again.
persist: true
(deprecated)
In previous versions, PartyKit only had one persistence mode:
This is functionally equivalent to setting the value to { mode: "history" }
.
This option is still supported for backwards compatibility reasons, but will be removed in a future version of y-partykit
.
Persisting to an external service
You can use a combination of the load
and callback
options to synchronise the document to an external service:
The load
callback is called on first connection to the server instance. Once the document has been loaded, itβs kept in memory until the session ends.
Learn more
For more information, refer to the official Yjs documentation. Examples provided in the Yjs documentation should work seamlessly with y-partykit
(ensure to replace y-websocket
with y-partykit/provider
).
Questions? Ideas? Weβd love to hear from you π Reach out to us on Discord or Twitter!