@livestore/sync-cf is the official LiveStore sync backend built on Cloudflare Workers and Durable Objects. It enables real-time synchronization across multiple clients using WebSocket connections.
The package has three entry points:
| Entry point | Purpose |
|---|---|
@livestore/sync-cf/cf-worker | Cloudflare Worker and Durable Object code (server side) |
@livestore/sync-cf/client | Sync client transports (browser/Node.js/Expo) |
@livestore/sync-cf/common | Shared types used by both sides |
Installation
- npm
- pnpm
- yarn
Client — @livestore/sync-cf/client
makeWsSync
Creates a WebSocket-based sync backend. Pass this to the adapter’ssync.backend option.
Parameters
URL of the sync backend. Accepts
http/https or ws/wss protocols.Enable WebSocket ping/pong keepalive.
How long to wait for a ping response before marking the connection as offline.
How often to send ping requests.
Optional custom WebSocket factory for environments with non-standard WebSocket APIs.
Example
Server — @livestore/sync-cf/cf-worker
makeDurableObject
Creates a Cloudflare Durable Object class that handles sync. One Durable Object instance is created perstoreId.
Options
Called when a client pushes events. Use this for server-side validation of individual push operations.
Called when a client requests to pull events.
Forward request headers into
onPush/onPull callbacks. Useful for cookie-based or header-based authentication inside the Durable Object.Storage engine for event persistence. Defaults to
{ _tag: 'do-sqlite' } (Durable Object SQLite).Use d1 when you want a centralized, externally queryable database at the cost of an extra network hop.Which transport protocols to accept. Defaults to all three.
OpenTelemetry configuration for the Durable Object.
Example Durable Object
worker.ts
makeWorker
Creates a Cloudflare Workerfetch handler that routes sync requests to the Durable Object. For simpler setups you can use this instead of writing a custom fetch.
Options
The Durable Object binding name declared in
wrangler.toml.Validates the client-provided payload during WebSocket connection. Throw to reject the connection.The
context includes storeId and headers (the raw request headers, regardless of forwardHeaders on the DO).Optional Effect Schema to decode the client payload before passing it to
validatePayload.Add CORS headers to responses.
Example Worker
worker.ts
handleSyncRequest
Lower-level alternative tomakeWorker. Handles a single LiveStore sync request. Use this when you have a custom fetch handler and want to forward only matching requests to LiveStore.
Wrangler configuration
wrangler.toml
Use
new_sqlite_classes (not new_classes) so the Durable Object gets the Durable Object SQLite storage backend.Full setup example
Peer dependencies
| Package | Version |
|---|---|
effect | ^3.19.19 |
@cloudflare/workers-types | 4.x |