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.
partysub adds scalable publish/subscribe messaging to Cloudflare Workers by using Durable Objects as coordinating nodes. Clients connect via PartySocket and subscribe to one or more named topics; any connected client (or an HTTP POST) can publish a message that fans out to all matching subscribers in real time.
Installation
Function: createPubSubServer
Import frompartysub/server. Call this once at the top level of your Worker to generate the Durable Object class and the request router.
Options
The exact name of the Durable Object binding declared in
wrangler.jsonc. The
client uses the lowercase form of this value as the party name.Number of Durable Object nodes to spin up per room id. More nodes allow
higher message throughput. Each node handles a subset of connections.
Optional map of Cloudflare location
hints
to node weights. The weight controls how many nodes are allocated to that
region. Clients connecting from a listed region are routed to a nearby node;
others are routed randomly.
Optional data jurisdiction (e.g.
"eu"). Cannot be combined with
locations. Not yet fully implemented.Return value
createPubSubServer returns an object with two exports:
The Durable Object class to export from your Worker and reference in
wrangler.jsonc.Routes incoming WebSocket upgrade requests and HTTP POST publish requests.
Returns
null when the request path does not match a pub/sub route, letting
you fall through to your own handlers.PubSubServer
ExportPubSubServer from your Worker entrypoint so the Cloudflare runtime can instantiate it as a Durable Object:
routePubSubRequest
fetch handler. It handles:
- WebSocket upgrades — clients connecting via
PartySocket - HTTP POST requests — server-side message publishing
wrangler.jsonc Configuration
DeclarePubSubServer as a Durable Object binding with SQLite storage and add a migration entry:
Client Connection with PartySocket
UsePartySocket to connect from the browser or Node.js. The party value must be the lowercase form of the binding name.
Lowercase form of the
binding name passed to createPubSubServer.The channel name. All clients in the same room share the same node pool.
Array of topic strings to subscribe to. Each entry is either an exact topic
name (e.g.
"topic-abc") or a prefix wildcard (e.g. "prefix:*"). Omit to
subscribe to all topics.Listening for messages
Publishing Messages
Over WebSocket
Send a JSON object withtopic and data fields:
Over HTTP (server-side publish)
UsePartySocket.fetch for server-to-client publishing without a persistent WebSocket connection:
The topic to publish on. Subscribers with matching exact or wildcard
subscriptions will receive the message.
The message payload. Can be any JSON-serializable value.
React: usePartySocket
Use theusePartySocket hook from partysocket/react for a React-friendly subscription: