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.
Server class is the core building block of PartyServer. Extend it to create a Durable Object that manages WebSocket connections with structured lifecycle hooks, built-in broadcasting, and an optional hibernation mode. Every server instance is addressed by name, making it straightforward to associate a server with a room ID, session, or any other application-level identifier.
Class definition
Server extends the Cloudflare DurableObject base class. The generic parameter Env is inferred from your Worker’s environment bindings as declared in wrangler.jsonc. The optional Props generic allows typed initial properties delivered by routePartykitRequest or getServerByName.
Static options
options.hibernate = true to enable WebSocket Hibernation. When hibernation is enabled the Durable Object is evicted from memory between messages, dramatically reducing CPU costs for low-traffic servers. All lifecycle hooks are called exactly as they would be in non-hibernating mode.
Subclasses inherit the nearest explicitly declared hibernate value — declaring partial options on a child class does not disable hibernation configured on a parent.
Lifecycle hooks
All lifecycle hooks are optional. Override only the ones your application needs. Every hook can be synchronous orasync.
onStart
this.name is available when onStart runs.
Optional initial properties delivered via
routePartykitRequest or getServerByName. Typed by the Props generic parameter of the class.onConnect
The newly established connection. See Connection for the full interface.
Contains the original HTTP upgrade
Request. See ConnectionContext.onMessage
The connection that sent the message.
The received message.
WSMessage is string | ArrayBuffer | ArrayBufferView.onClose
The connection that was closed.
The WebSocket close code sent by the peer (e.g.
1000 for a normal closure).Human-readable reason string accompanying the close code.
true if the connection was closed via the proper WebSocket closing handshake.onError
The connection on which the error occurred.
The error value. May be an
Error instance or a raw value depending on the runtime.onRequest
Response. The default implementation returns 404 Not implemented.
The incoming HTTP request.
The HTTP response to send to the client.
onAlarm
this.ctx.storage.setAlarm(date). Read more about Durable Object alarms.
Do not override the
alarm() method directly — override onAlarm() instead. PartyServer’s alarm() implementation calls onAlarm() after ensuring the server is fully initialized.getConnectionTags
getConnections(). Each connection supports up to 9 tags and each tag may be at most 256 characters long. The default implementation returns an empty array.
The connection being accepted.
Contains the original HTTP upgrade
Request, useful for reading auth tokens or query parameters to determine which tags to apply.Tags to associate with the connection. Retrievable later via
connection.tags and filterable via getConnections(tag).Instance methods
broadcast
without array to exclude specific connections from the broadcast — useful for excluding the sender.
The message payload to send.
Optional list of connection IDs to skip. Connections whose
id appears in this array will not receive the message.getConnections
tag string to return only connections that were assigned that tag by getConnectionTags.
Optional tag to filter by. When omitted, all connections are returned.
An iterable of matching
Connection objects, each typed with TState if provided.getConnection
undefined if no connection with that ID is currently active.
The unique connection ID to look up.
The matching
Connection, or undefined if not found.sql
T.
Properties
name
this.ctx.id.name — the native Durable Object ID name populated whenever the stub was addressed via idFromName() or getByName(). Available in every entry point including the constructor, onStart(), onAlarm(), and hibernating WebSocket handlers.
PartyServer also persists a
__ps_name fallback record during initialization so that alarm handlers firing on stale on-disk alarm records from older workerd versions can still recover the name.Accessing .name on a DO addressed via idFromString() or newUniqueId() without a setName() bootstrap will throw.ctx
DurableObject base class. Provides access to ctx.storage (Transactional Storage API), ctx.waitUntil(), and other runtime primitives.
env
wrangler.jsonc — KV namespaces, R2 buckets, AI bindings, other Durable Object namespaces, and so on.
Durable Object methods — do not override
These methods are implemented by PartyServer and must not be overridden in subclasses. Override the corresponding lifecycle hooks instead.Overriding any of these methods will break PartyServer’s connection management, hibernation support, or initialization logic.
fetch
fetch to route WebSocket upgrade requests through onConnect and plain HTTP requests through onRequest. If you must implement fetch yourself (e.g. to intercept requests before any lifecycle methods run), call super.fetch(request) at the appropriate point to preserve lifecycle behavior.
alarm
onAlarm(). Do not override — use onAlarm() instead.
webSocketMessage / webSocketClose / webSocketError
These three Durable Object hibernation API methods are overridden by PartyServer to dispatch to onMessage, onClose, and onError respectively. Do not implement them on your subclass.