Party.Server (Server API)
Party.Server
Each PartyKit server is a TypeScript module that implements the Party.Server
interface.
Note: Previously, PartyKit supported an alternative export default {} satisfies PartyKitServer
syntax. You can read the API documentation for the legacy syntax here
new Party.Server (constructor)
The Party.Server
constructor receives an instance of Party.Room
, which gives you access to the room state and resources such as storage, connections, id, and more.
Party.Server.options
You can define an options
field to customise the PartyServer behaviour.
Party.Server.options.hibernate
Whether the PartyKit platform should remove the server from memory between HTTP requests and WebSocket messages. The default value is false
.
Related guide: Scaling PartyKit Servers with Hibernation
Party.Server.onStart
Called when the server is started or after waking up from hibernation, before first onConnect
or onRequest
.
You can use this to load data from storage and perform other asynchronous initialization, such as retrieving data or configuration from other services or databases.
Party.Server.onConnect
Called when a new incoming WebSocket connection is opened.
Receives a reference to the connecting Party.Connection
, and a Party.ConnectionContext
that provides information about the initial connection request.
Related guide: Building a Real-time WebSocket server
Party.Server.onMessage
Called when a WebSocket connection receives a message from a client, or another connected party.
Receives the incoming message, which can either be a string or a raw binary ArrayBuffer, and a reference to the sending Party.Connection
.
Party.Server.onClose
Called when a WebSocket connection is closed by the client.
Receives a reference to the closed Party.Connection
. By the time onClose
is called, the connection is already closed and can no longer receive messages.
Party.Server.onError
Called when a WebSocket connection is closed due to a connection error.
Receives a reference to the closed Party.Connection
, and an Error
object.
Party.Server.onRequest
Called when a HTTP request is made to the room URL.
Receives an instance of Party.Request
, and is expected to return a standard Fetch API Response
.
Related guide: Responding to HTTP requests
Party.Server.onAlarm
Called when an alarm is triggered.
Alarms have access to most Room
resources such as storage, but not Room.id
and Room.context.parties
properties. Attempting to access them will result in a runtime error.
Related guide: Scheduling tasks with Alarms
Party.Server.getConnectionTags
You can set additional metadata on connections by returning them from a getConnectionTags
, and then filter connections based on the tag with Party.getConnections
.
static
onBeforeRequest
Runs before any HTTP request is made to the party. You can modify the request
before it is forwarded to the party, or return a Response
to short-circuit it.
Receives an instance of Party.Request
, and is expected to either return a request, or a standard Fetch API Response
.
Because the static onBeforeRequest
method runs in an edge worker near the user instead of in the room, it doesn’t have access to Party
room resources such as storage. Instead, you can access a subset of its properties via a Party.Lobby
.
Related reading: How PartyKit works
static
onBeforeConnect
Runs before any WebSocket connection is made to the party. You can modify the request
before it is forwarded to the party, or return a Response
to prevent the connection
Receives an instance of Party.Request
, and is expected to either return a request, or a standard Fetch API Response
.
Because the static onBeforeConnect
method runs in an edge worker near the user instead of in the room, it doesn’t have access to Party
room resources such as storage. Instead, you can access a subset of its properties via a Party.Lobby
.
Related reading: How PartyKit works
Party.Worker
The Party.Worker
interface describes the static
methods available on the Party.Server
class. You can use it to add additional type safety to your TypeScript code:
static
onFetch
Runs on any HTTP request that does not match a Party URL or a static asset. Useful for running lightweight HTTP endpoints that don’t need access to the Party
state.
Receives an instance of Party.Request
, and is expected to either return a standard Fetch API Response
.
Because the static onFetch
method runs in an edge worker near the user instead of in the room, it doesn’t have access to Party
room resources such as storage. Instead, you can access a subset of its properties via a Party.FetchLobby
.
Related reading: Creating custom endpoints with onFetch
static
onSocket
Runs on any WebSocket connection that does not match a Party URL. Useful for running lightweight WebSocket endpoints that don’t need access to the Party
state.
Receives an instance of Party.FetchSocket
.
static
onCron
Runs on a schedule defined in the crons
field of the partykit.json
file. Useful for running code periodically, such as sending reminders or cleaning up old data.
Receives an instance of Party.Cron
.
Party.Room
Each Party.Server
instance receives an instance of Party.Room
as a constructor parameter, and can use it to access the room state and resources such as storage, connections, id, and more.
Room.id
Room ID defined in the Party URL, e.g. /parties/:name/:id
.
Room.internalID
Internal ID assigned by the platform. Use Room.id
instead.
Room.env
Environment variables defined for this project.
Related reading: Managing environment variables with PartyKit.
Room.storage
A per-room, asynchronous key-value storage.
- The key must be a string with a max size of 2,048 bytes.
- The value can be any type supported by the structured clone algorithm, limited to 128 KiB (131,072 bytes) per value
Related reading: Persisting state into storage.
Room.context
Additional information about other resources in the current project.
Room.context.parties
Access other parties in this project.
Read more: Using multiple parties per project
Room.context.ai
Access to the AI binding defined in the project.
Related reading: PartyKit AI
Room.context.vectorize
Access to Vectorize Indexes defined in the project.
Related reading: PartyKit AI
Room.broadcast
Send a message to all connected clients, except connection ids listed in the second array parameter.
Related guide: Building a Real-time WebSocket server
Room.getConnection
Get a connection by connection id. Returns a PartyConnection
undefined
if connection by id doesn’t exist.
Room.getConnections
Get all currently connected WebSocket connections. Returns an iterable list of PartyConnection
s.
Optionally, you can provide a tag to filter returned connections.
Use PartyServer.getConnectionTags
to tag the connection when the connection is made.
Party.Connection
Wraps a standard WebSocket
, with a few additional PartyKit-specific properties.
Party.Connection.id
Uniquely identifies the connection. Usually an automatically generated GUID, but can be specified by the client by setting an id
property on PartySocket.
Party.Connection.uri
The original URI of the connection request.
Party.Connection.setState
setState
allows you to store small pieces of data on each connection.
Unlike Room.storage
, connection state is not persisted, and will only exist for the lifetime of the WebSocket connection.
Party.Connection.state
Read state stored with Party.Connection.setState
.
Party.Connection.serializeAttachment
Deprecated. Use Party.Connection.setState
instead.
Party.Connection.deserializeAttachment
Deprecated. Use Party.Connection.state
instead.
Party.Request
Wraps an underlying Cloudflare runtime Request.
Party.Lobby
Provides access to a limited subset of room resources for onBeforeConnect
and onBeforeRequest
methods.
Party.Lobby.id
See: Room.id
Party.Lobby.env
See: Room.env
Party.Lobby.parties
See: Room.context.parties
Party.Lobby.ai
Access to the AI binding defined in the project.
Related reading: PartyKit AI
Party.Lobby.vectorize
Access to Vectorize Indexes defined in the project.
Related reading: PartyKit AI
Party.FetchLobby
Provides access to a limited subset of project resources for the onFetch
method:
FetchLobby.env
Environment variables defined for this project.
Related reading: Managing environment variables with PartyKit.
FetchLobby.parties
Access other parties in this project.
Read more: Using multiple parties per project
FetchLobby.ai
Access to the AI binding defined in the project.
Related reading: PartyKit AI
FetchLobby.vectorize
Access to Vectorize Indexes defined in the project.
Related reading: PartyKit AI
Party.FetchSocket
Wraps a standard WebSocket
, with an additional request
property that contains the original HTTP request.
Party.Cron
Describes a cron job that is about to be executed. You can use it to access the cron’s name, definition and scheduledTime.
Party.CronLobby
Provides access to a limited subset of project resources for the onCron
method:
CronLobby.env
Environment variables defined for this project.
Related reading: Managing environment variables with PartyKit.
CronLobby.parties
Access other parties in this project.
Read more: Using multiple parties per project
CronLobby.ai
Access to the AI binding defined in the project.
Related reading: CronLobby AI
CronLobby.vectorize
Access to Vectorize Indexes defined in the project.
Related reading: PartyKit AI