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.
partysync is a library for synchronizing typed records from a single Durable Object to a client in real time. You define a schema of record and action types, the server handles each action and returns changed records, and the client receives live updates — with optional optimistic updates that apply immediately before the server confirms.
Installation
When to use partysync
partysync is a good fit when:
- You have one Durable Object per user or entity and want to stream its entire state to the client.
- You want the guarantees Durable Objects provide: consistency, hibernation, and single-writer semantics.
- The total state is too large to hold in the client’s memory.
- You need to sync slices of a shared relational database (Postgres, MySQL). For those cases, consider zero, TinyBase, ElectricSQL, or similar.
Schema Definition
Apartysync schema is a Record mapping channel names to a two-element tuple: [RecordType, ActionType].
RecordType is a tuple whose fields match the columns of your SQLite table. It must always start with an id string and end with a nullable deleted_at number:
ActionType is a discriminated union of every mutation the client can request:
Class: SyncServer<Env, Schema>
Import frompartysync. Extend SyncServer to create your Durable Object server.
Your Worker environment bindings type.
Maps each channel name to its record type and action type tuple. You can
define multiple channels in a single
SyncServer.onStart()
Called once when the Durable Object starts. Use it to create the SQLite tables that back your channels with this.ctx.storage.sql.exec.
onAction(channel, action)
Called whenever a client sends an action. Return the records that changed — partysync will broadcast them to all connected clients.
The channel the action was sent on, e.g.
"todos".The action dispatched by the client. Matches the
ActionType defined in your
schema for the given channel.RecordType rows (or a Promise of one). Every returned record is broadcast to all clients subscribed to that channel.
Hook: useSync
Import frompartysync/react. Subscribes to a channel over a WebSocket and returns the current record list together with a function to dispatch actions.
Signature
The channel name to subscribe to. Must match a key in the server’s
Schema.An open WebSocket (e.g. from
PartySocket) connected to the SyncServer.Optional. Called immediately when
sendAction is invoked, before the server
confirms. Return the expected new state of the record array. If the server
returns different data, the optimistic state is replaced.The current list of records for the channel, kept in sync with the server.
Dispatches an action to the server. If
optimisticUpdate is provided, the UI
updates immediately while the server processes the action.