ProtectedctxProtectedenvReadonlyloggerThe main logger, logging messages to console and storage.
The interval that ticks the room every second.
OptionalvalidOnly set, if this room was created by a request to /createRoom
StaticoptionsThe name for this server.
Resolves from this.ctx.id.name — the native DO id name, populated
whenever the stub was created via idFromName() or getByName().
This is available inside every entry point (including the constructor,
alarms, and hibernating websocket handlers).
For alarm handlers firing on stale on-disk alarm records from
older workerd versions that didn't persist name into the alarm
record, the name is recovered from a storage fallback record.
Throws if neither source is available — typically this means the DO
was addressed via idFromString() or newUniqueId(), which is not
supported by PartyServer.
The alarm handler invoked by the Cloudflare Workers runtime.
Send a message to all connected clients, except connection ids listed in without
Optionalwithout: string[]Cancels a scheduled event by its identifier and removes it from storage.
The unique identifier of the event to cancel.
true if the event existed and was removed, false if not
OptionalconnectCreates a ValidRoom instance, allowing players to connect to this room.
Calculates the current number of active WebSocket connections with the "player" tag in the room.
The count of connected clients.
Get a connection by connection id
Get all connections. Optionally, you can provide a tag to filter returned connections.
Use Server#getConnectionTags to tag the connection on connect.
Optionaltag: stringYou can tag a connection to filter them in Server#getConnections. Each connection supports up to 9 tags, each tag max length is 256 characters.
Checks whether a connection has a specific connection tag assigned by getConnectionTags.
The connection to check.
The tag that should be present.
whether the connection has the tag or not.
Returns whether this room is valid or not.
Handles a WebSocket connection that was closed.
The connection that closed.
Handles a new WebSocket connection to the room.
The new connection.
The connection context.
Handles errors for a connection. Currently just logs them as warnings.
the connection that had an error
the error that occured
Called when an exception occurs.
the error that occurred
Handles incoming messages from a WebSocket connection.
The connection that sent the message.
The message content as a string.
Called when the server is started for the first time.
Optionalprops: Record<string, unknown>Called every second as soon as the first player connects.
Processes an individual scheduled event.
The event object to be processed.
Restores the state of the room, e.g. when the Durable Object gets restarted.
Broadcasts a message to all connected clients, optionally filtered by a specific tag.
The message object to be broadcasted to the connections.
URL to load music from.
Whether to perform advanced filtering tactics when generating the songs array. Currently just ignores parens when filtering for identical song names.
The music start position.
Used as default value in PlayerPicksGame, used as forced position in MultipleChoiceGame.
Whether to prefer distractions by the same artist.
Whether to directly end the round after all players answered.
The game mode being played.
The amount of time a player should have to pick a song.
The amount of rounds to play.
The time per question in each round.
A map of all active (online, non-spectating) players. Key is server generated uuid, NOT connection id.
The current game state
The player's unique identifier generated by the server. Not connection id!
The current version of the server.
The count of filtered songs.
Optionalplaylists?: {Currently selected playlist(s)
The current countdown number. 0 to hide.
The current game phase.
Optionalquestion?: The round number.
The songs that were played in this round.
The added log message.
The log level of the added message.
The duration of the progressbar.
The number of seconds to offset the progress bar start point.
Optionalerror?: stringOptional error message if the requested action could not be performed.
The message that is being confirmed.
The sequence number the pong should respond with
The sequence number asked for in the ping packet.
Optionaltag: stringAn optional filter to target a specific subset of connections.
Safely sends a JSON-serialized message over a connection if it is currently open.
The active party connection object used to send the data.
The message object to be stringified and transmitted to the client.
URL to load music from.
Whether to perform advanced filtering tactics when generating the songs array. Currently just ignores parens when filtering for identical song names.
The music start position.
Used as default value in PlayerPicksGame, used as forced position in MultipleChoiceGame.
Whether to prefer distractions by the same artist.
Whether to directly end the round after all players answered.
The game mode being played.
The amount of time a player should have to pick a song.
The amount of rounds to play.
The time per question in each round.
A map of all active (online, non-spectating) players. Key is server generated uuid, NOT connection id.
The current game state
The player's unique identifier generated by the server. Not connection id!
The current version of the server.
The count of filtered songs.
Optionalplaylists?: {Currently selected playlist(s)
The current countdown number. 0 to hide.
The current game phase.
Optionalquestion?: The round number.
The songs that were played in this round.
The added log message.
The log level of the added message.
The duration of the progressbar.
The number of seconds to offset the progress bar start point.
Optionalerror?: stringOptional error message if the requested action could not be performed.
The message that is being confirmed.
The sequence number the pong should respond with
The sequence number asked for in the ping packet.
Whether to log the sended message
Saves relevant state of the room to storage.
(Re-)Schedules a cleanup after ROOM_CLEANUP_TIMEOUT seconds.
(Re-)Schedules a one-time or recurring event.
The unique identifier for the event.
The time (in milliseconds) after which the event should execute.
The repetition interval in milliseconds, or null if one-time.
(Re-)Schedules a host transfer to another player after ROOM_HOST_TRANSFER_TIMEOUT seconds.
Establish this server's name and trigger onStart().
Use cases:
ctx.id.name is
undefined — e.g. DOs addressed via idFromString() /
newUniqueId(). setName() stashes the name in memory and
persists it under __ps_name so cold-wake invocations
recover it via #ensureInitialized()'s legacy fallback.props to onStart() via the
optional second argument.For DOs addressed via idFromName() / getByName(), calling
setName() is redundant — this.name is available automatically
from ctx.id.name. The normal initialization path also persists
a fallback record so old-compat alarm handlers can recover the name.
Throws if name does not match ctx.id.name.
Not appropriate for facets. Cloudflare Agents and any other
framework using ctx.facets.get(...) should pass an explicit
id in FacetStartupOptions so the facet has its own
ctx.id.name:
const stub = ctx.facets.get(facetKey, () => ({
class: ChildClass,
id: ctx.exports.SomeBoundDOClass.idFromName(facetName),
}));
Without an explicit id, the facet inherits the parent DO's
ctx.id (including ctx.id.name), and setName() will throw
the ctx.id.name-mismatch error because the facet's intended
name differs from the parent's. See
https://developers.cloudflare.com/dynamic-workers/usage/durable-object-facets/
for the FacetStartupOptions.id semantics.
Optionalprops: Record<string, unknown>Execute SQL queries against the Server's database
Type of the returned rows
SQL query template strings
Values to be inserted into the query
Array of query results
Stops the tick loop, if running.
Creates a new room server.