The PartyServer ecosystem ships first-class React hooks across several packages. Each hook manages socket lifecycle (creation, reconnection, and cleanup) insideDocumentation 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.
useEffect, so your components stay declarative. All hooks require React 18+ and are ESM-only.
usePartySocket — partysocket/react
usePartySocket is the primary hook for connecting a React component to a PartyServer room. It wraps PartySocket and keeps the underlying socket stable across renders: the socket is only recreated when connection-identity options (host, room, party, path, protocol) change — not on every render.
Options
| Option | Type | Description |
|---|---|---|
host | string | Server host. Defaults to window.location.host. |
room | string | The room (Durable Object name) to connect to. |
party | string | The party namespace. Defaults to "main". |
id | string | Client connection ID. A random UUID is generated if omitted. |
path | string | Additional path appended to the WebSocket URL. |
query | Params | () => Params | Promise<Params> | Query parameters. Functions are re-evaluated on each reconnect. |
protocol | "ws" | "wss" | Force a specific WebSocket protocol. |
protocols | ProtocolsProvider | Sub-protocols passed to the WebSocket constructor. |
basePath | string | Base path prefix for the party URL. |
prefix | string | Alternative URL prefix. |
enabled | boolean | Set to false to skip connecting. Defaults to true. |
onOpen | (event) => void | Fired when the connection opens. |
onMessage | (event) => void | Fired on each incoming message. |
onClose | (event) => void | Fired when the connection closes. |
onError | (event) => void | Fired on connection error. |
transferEnqueuedMessages | boolean | undefined | Controls whether messages buffered while disconnected are transferred to a new socket when options change. |
Stable socket behavior
usePartySocket serializes connection-identity options into a memo key. The socket is only replaced (and onOpen re-fired) when one of these values changes: host, room, party, path, protocol, protocols, basePath, prefix, id, or query. Event handlers (onMessage, onOpen, etc.) are attached via a ref so you can update them without triggering a reconnect.
enabled flag
Set enabled: false to defer the connection until some condition is met (e.g. the user is authenticated):
useWebSocket — partysocket/use-ws
useWebSocket is a lower-level hook that wraps the reconnecting WebSocket class from partysocket without the PartyServer URL conventions. Use it when you need a resilient WebSocket to an arbitrary URL.
url and protocols arguments as the WebSocket constructor, plus all the reconnecting-websocket Options and event handlers from EventHandlerOptions.
useYProvider — y-partyserver/react
useYProvider creates and manages a YProvider instance for Yjs collaborative editing. It handles provider creation and cleanup automatically.
options, persistence hooks, and custom messaging.
useSync — partysync/react
useSync is an experimental hook from partysync that synchronizes typed record arrays between a SyncServer Durable Object and a React component. It returns a tuple of [records, dispatch] — similar to useReducer — with optimistic updates applied immediately on the client.
partysync is experimental and its API is subject to change. It is best suited for syncing small record sets from a single Durable Object per user.Observable hooks — partytracks/react
partytracks is a WebRTC/audio/video library for Cloudflare Realtime SFU. Its React helpers expose three low-level Observable utility hooks that bridge RxJS Observables into React state.
useObservableAsValue
Subscribes to an Observable and returns its latest emitted value as React state. Unsubscribes automatically on unmount.
undefined.
useObservable
Subscribes to an Observable with a full observer. Useful when you need error or complete callbacks, or want to call setState yourself.