TheDocumentation 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.
partysocket/react module provides React hooks that manage the full lifecycle of a PartySocket or WebSocket connection — creating the socket on mount, reconnecting when connection options change, and cleaning up on unmount. Event handlers (onMessage, onOpen, etc.) are attached in a React-friendly way so you can safely reference state and props from inside them without stale-closure issues.
usePartySocket
Signature
PartySocket instance. The socket is stable across
re-renders when connection options are unchanged — you can safely store it in
state or pass it to child components.
Options
UsePartySocketOptions combines PartySocketOptions (minus the required host, which becomes optional here) with event handler callbacks and two lifecycle controls:
Connection destination
Base URL for the party server. When omitted inside a browser, defaults to
window.location.host so the socket connects to the current page origin.Room name to connect to.
Party binding name. Defaults to
"main".Client connection ID. A UUID v4 is generated automatically when omitted.
Additional path segment appended after the room.
Overrides the entire
parties/<party>/<room> path prefix.Overrides only the
parties segment of the default path.Explicit WebSocket protocol override.
WebSocket sub-protocol(s) or a (possibly async) function that returns them.
Query parameters to include in the URL. Pass a function for dynamic or
async values such as auth tokens.
Suppress warnings when
party or room contain a forward slash.Lifecycle controls
When
false, the socket is closed and held open until enabled becomes
true again. Toggling enabled does not create a new socket instance —
it calls close() / reconnect() on the existing one. Default: true.Controls what happens to messages buffered by
send() when the hook must
replace the socket because connection options changed:undefined(default) — transfer messages only when the destination is unchanged (i.e. only credential-style options likequerychanged). If the room, party, host, or path changed, buffered messages are discarded with a warning.true— always transfer buffered messages to the new socket.false— always discard buffered messages, with a warning.
Reconnection options
All standardReconnectingWebSocket options are also accepted: maxReconnectionDelay, minReconnectionDelay, reconnectionDelayGrowFactor, minUptime, connectionTimeout, maxRetries, maxEnqueuedMessages, startClosed, shouldReconnectOnClose, debug, debugLogger, WebSocket.
Changing any reconnection option causes the hook to create a new socket with
the updated configuration.
Event handlers
Called when the connection opens. The latest callback is always used —
no need to unsubscribe and re-subscribe when it changes.
Called when a message is received from the server.
Called when the connection closes (whether cleanly or not).
Called when a connection error occurs.
Reconnect behaviour
The socket is replaced (a newPartySocket instance is created) when any of
the following change:
host,room,party,path,basePath,prefix,protocol,protocols,query,id- Any reconnection option (
maxRetries,connectionTimeout, etc.)
onMessage / onOpen / onClose / onError callback references change — handlers are always updated in place. Similarly, toggling enabled reuses the existing socket.
If
query is passed as a function, changing the function’s identity (e.g. a
new inline arrow function each render) will cause a reconnect. Wrap the
function with useCallback or define it outside the component to stabilise
its reference.useWebSocket
Signature
WebSocket class (i.e. ReconnectingWebSocket) directly, without the PartyKit room URL-building logic. Use this when you want React lifecycle management for an arbitrary WebSocket endpoint.
The endpoint to connect to: a
string, () => string, or
() => Promise<string>. Changing the URL causes the hook to replace the
socket.Sub-protocol(s) or a provider function. Changing this also triggers a
socket replacement.
Accepts all
Options from
ReconnectingWebSocket plus enabled, transferEnqueuedMessages, and the
same onOpen, onMessage, onClose, onError event handlers as
usePartySocket.WebSocket instance.
Usage examples
Chat room component
Sending and receiving messages with state
Connecting to the current origin
Whenhost is omitted in a browser, the hook defaults to
window.location.host, so you can connect without hard-coding a URL: