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.
WebSocket named export from partysocket is a drop-in replacement for the standard browser WebSocket API. It automatically reconnects when a connection drops, buffers messages sent while disconnected, supports dynamic and async URL providers, and works across browsers, Node.js, React Native, Cloudflare Workers, Deno, and Bun — without any dependencies on Window, the DOM, or an EventEmitter library.
Import
This is the
ReconnectingWebSocket class re-exported under the familiar
WebSocket name. It is intentionally API-compatible with the browser’s built-in
WebSocket so you can swap it in with minimal changes.Constructor
The WebSocket endpoint to connect to. Can be a plain string, a function that
returns a string, or an async function that resolves to a string. The
function is re-evaluated before every connection attempt, making it easy to
inject fresh tokens or rotate between endpoints.
Optional sub-protocol(s). Accepts a static value or a (possibly async)
function that returns one before each connection attempt. See
ProtocolsProvider below.
Types
UrlProvider
| Form | When to use |
|---|---|
string | Static endpoint that never changes. |
() => string | Dynamic URL computed synchronously (e.g. round-robin). |
() => Promise<string> | Async URL — e.g. fetch a short-lived signed URL or auth token before each connect. |
ProtocolsProvider
UrlProvider in flexibility. Pass null to
opt out of sub-protocols, or a function to generate them fresh before each
connection.
Options
Custom WebSocket constructor. Required in environments without a global
WebSocket, such as older Node.js. Install the ws package and pass it
here.Maximum delay between reconnection attempts in milliseconds. Default:
10000.Minimum delay between reconnection attempts in milliseconds. Default:
3000.Factor by which the reconnection delay grows after each failed attempt.
Default:
1.3.Minimum time in milliseconds a connection must remain open to be considered
stable and reset the retry counter. Default:
5000.Milliseconds to wait for
open before treating the attempt as a timeout
and retrying. Default: 4000.Maximum total reconnection attempts before giving up. Default:
Infinity.Maximum number of messages to buffer while disconnected. Default:
Infinity.When
true, the socket starts in CLOSED state and does not connect until
reconnect() is called explicitly. Default: false.Called on each close event. Return
false to prevent automatic
reconnection for that specific close (e.g. intentional server-side
termination).Enables verbose internal logging. Default:
false.Custom log function used when
debug is true. Defaults to
console.log.Properties
The URL as reported by the underlying
WebSocket. Empty string while no
connection exists.Number of reconnection attempts made since the last stable open.
Total byte size of messages queued via
send() but not yet transmitted,
including both the internal queue and the underlying socket’s buffer.How binary messages are received (
"blob" or "arraybuffer"). Default:
"blob".The sub-protocol selected by the server, or an empty string.
Extensions negotiated by the server, or an empty string.
true while the socket is in automatic-reconnect mode. Becomes false
after close() is called.Inline event handler for the
open event.Inline event handler for the
message event.Inline event handler for the
close event.Inline event handler for the
error event.Constants
| Constant | Value | Description |
|---|---|---|
WebSocket.CONNECTING | 0 | Connection not yet open. |
WebSocket.OPEN | 1 | Connection open and ready. |
WebSocket.CLOSING | 2 | Closing handshake in progress. |
WebSocket.CLOSED | 3 | Connection closed or could not open. |
Methods
send
true if sent immediately over an open connection,
false if the message was queued. Buffered messages are flushed before the
next open event.
close
close event fires synchronously.
reconnect
startClosed: true or to force a fresh connection.
drainQueuedMessages
send() that were never
transmitted. Useful when swapping out a socket instance and wanting to forward
unsent messages to its replacement.