PartyServer represents each WebSocket client as aDocumentation 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.
Connection — a standard WebSocket extended with an ID, tags, per-connection state, and a server name. Connections are created automatically when a client upgrades to WebSocket and are passed to every relevant lifecycle hook. Understanding the Connection interface is key to building stateful, multi-client servers.
Connection
Connection extends the platform WebSocket type, so all standard WebSocket methods and events (send, close, addEventListener, etc.) are available in addition to the PartyServer-specific fields below.
The generic parameter TState types the state property and the argument to setState. Use it to avoid unsafe casts when accessing per-connection data:
A unique identifier for this connection, generated by PartyServer (using nanoid) when the WebSocket is accepted. Clients can supply their own ID via the
_pk query parameter; otherwise one is auto-generated. Stable for the lifetime of the connection.The URL of the original WebSocket upgrade request. Persisted in the WebSocket attachment so it survives hibernation and can be read in hibernating event handlers.
An array of tags assigned to this connection by
getConnectionTags. Use these to group or categorize connections (e.g. by room, role, or subscription). Read-only — tags are set at accept time and cannot be changed afterwards.The name of the
Server instance this connection belongs to — equivalent to this.name on the server. Populated after the server is initialized.This field is deprecated. Prefer reading
this.name directly on your Server subclass instead of accessing it from the connection object.Arbitrary state associated with this connection. Read-only — use
setState to update it. Persisted in the WebSocket attachment so it survives hibernation. The maximum serialized size is 2 KB.send
Inherited from the platform WebSocket. Send a message to this specific connection.
The message payload to deliver to this client.
close
Inherited from the platform WebSocket. Close this specific connection.
Optional WebSocket close code (e.g.
1000 for normal closure, 4000–4999 for application-defined codes).Optional human-readable reason string. Maximum 123 bytes (UTF-8).
setState
The new state value, or an updater function. Pass
null to clear the state.The updated state (an immutable view of the value you passed).
ConnectionContext
onConnect and getConnectionTags. Provides access to the original HTTP upgrade request so you can read headers, query parameters, cookies, or other request metadata at connection time.
The original HTTP upgrade request that initiated the WebSocket connection. Use this to read authentication tokens, session cookies, or URL query parameters.
ConnectionState
ConnectionState<T> is a deeply immutable view of the state value stored on a connection. It is null before any state has been set. The underlying value must be JSON-serializable because it is stored in the WebSocket attachment and survives hibernation.
The maximum serialized size of connection state is 2 KB. Storing larger values will fail silently or cause errors at runtime. For larger datasets, use Durable Object storage via
this.ctx.storage on the server.WSMessage
onMessage and accepted by broadcast and connection.send. Covers all message formats a WebSocket can carry:
| Variant | Description |
|---|---|
string | UTF-8 text message (most common for JSON payloads) |
ArrayBuffer | Raw binary message |
ArrayBufferView | A view into an ArrayBuffer, e.g. Uint8Array |