@playerthree/colyseus
@playerthree/colyseus — bind a Colyseus Room to a PlayerThree PlayerThreeMatch.
What the binding does (and nothing game-specific):
onAuth: atomically claims a host authorization lease for this connection and match. Every inbound request and private delivery reauthorizes that lease; revocation closes the connection.- routes
MSG.call | stream | mandate | scopes | resolveApproval | controller | mint | telltomatch.handleMessagewith a size cap and an in-flight cap per client; replies onMSG.result, stream pushes onMSG.obs. - fan-out of each team's view stream (
match.onView/match.viewMessages): a keyframe on join or watch, then the structural deltas, each message encoded once and shared by every reader of that team. Each team's guarded view goes only to that team's owner clients and to spectators that chose to watch it (MSG.watch, rate limited per spectator token). Seat principals (agents) never receive views — only their own stream. A team's stream is subscribed (computed) only while some client watches it. - view flow control, for every client: a client gets views about once per
viewIntervalMsat most, and each batch is followed by a WebSocket ping: no further views until its pong shows the client has read up to there. A slow client then catches up with the deltas it missed in one batch, or with one keyframe when it fell more than a bounded batch behind, instead of backing up its socket until the transport's heartbeat drops it as unresponsive. Nothing else (join/watch keyframes, seats, results, stream pushes, reports, chat) is held back. - pushes
MSG.seatsandMSG.cards(on join, on change) andMSG.report(once a team's outcome is decided). - observer principals choose a granted perspective with
MSG.observerand receive only its released observer frames; they get no views, seats or RPC. - chat mode: routes owner messages and agent replies only to the exact owning human seat (or a trusted private team-root client), never to spectators, agents, opponents or another human role on the same team.
- cleans up subscriptions on leave and on dispose.
Wire it into your room's lifecycle hooks:
class MyRoom extends Room {
playerThree!: PlayerThreeRoomBinding;
onCreate(options) { this.playerThree = bindPlayerThreeRoom(this, { host, match: host.getMatch(options.matchId)! }); }
onAuth(client, options) { return this.playerThree.onAuth(client, options); }
onJoin(client, options) { this.playerThree.onJoin(client, options); }
onLeave(client) { this.playerThree.onLeave(client); }
onDispose() { this.playerThree.dispose(); }
}Interfaces
PlayerThreeHostPort
Defined in: packages/colyseus/src/index.ts:86
The parts of a PlayerThreeHost the binding uses (every PlayerThreeHost<M> is assignable).
Methods
authenticate()
authenticate(token):
| {
match: {
id: string;
};
principal: Principal;
}
| undefined;Defined in: packages/colyseus/src/index.ts:87
Parameters
| Parameter | Type |
|---|---|
token | string |
Returns
| {
match: {
id: string;
};
principal: Principal;
}
| undefined
authorizeLease()
authorizeLease(lease): Principal | undefined;Defined in: packages/colyseus/src/index.ts:89
Parameters
| Parameter | Type |
|---|---|
lease | AuthorizationLease |
Returns
Principal | undefined
claimToken()
claimToken(token, connectionId):
| AuthorizationLease
| undefined;Defined in: packages/colyseus/src/index.ts:88
Parameters
| Parameter | Type |
|---|---|
token | string |
connectionId | string |
Returns
| AuthorizationLease
| undefined
disconnectLease()
disconnectLease(lease): void;Defined in: packages/colyseus/src/index.ts:90
Parameters
| Parameter | Type |
|---|---|
lease | AuthorizationLease |
Returns
void
onLeaseRevoked()
onLeaseRevoked(listener): () => void;Defined in: packages/colyseus/src/index.ts:91
Parameters
| Parameter | Type |
|---|---|
listener | (lease) => void |
Returns
() => void
PlayerThreeMatchPort
Defined in: packages/colyseus/src/index.ts:61
The parts of a PlayerThreeMatch the binding uses, with teams as plain strings so any game's match fits
(every PlayerThreeMatch<M> is assignable).
Properties
| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
id | readonly | string | - | packages/colyseus/src/index.ts:62 |
teams | readonly | readonly string[] | The teams taking part (a subset of the manifest teams): spectator watches, reports and perspectives cover only these. | packages/colyseus/src/index.ts:64 |
Methods
cards()
cards(): PublicSeatCard[];Defined in: packages/colyseus/src/index.ts:66
Returns
dispose()
dispose(): void;Defined in: packages/colyseus/src/index.ts:82
Returns
void
handleMessage()
handleMessage(
principal,
msg,
push?
): Promise<ServerMsg | undefined>;Defined in: packages/colyseus/src/index.ts:80
Parameters
| Parameter | Type |
|---|---|
principal | Principal |
msg | unknown |
push? | (msg) => void |
Returns
Promise<ServerMsg | undefined>
humanJoined()
humanJoined(
team,
role,
joined
): void;Defined in: packages/colyseus/src/index.ts:67
Parameters
| Parameter | Type |
|---|---|
team | string |
role | string |
joined | boolean |
Returns
void
observerMessages()
observerMessages(
principal,
perspective,
after?
): ObserverMessage[];Defined in: packages/colyseus/src/index.ts:72
Parameters
| Parameter | Type |
|---|---|
principal | { grant: ObserverGrant; kind: "observer"; match: string; } |
principal.grant | ObserverGrant |
principal.kind | "observer" |
principal.match | string |
perspective | string |
after? | number |
Returns
onEnd()
onEnd(listener): () => void;Defined in: packages/colyseus/src/index.ts:77
Parameters
| Parameter | Type |
|---|---|
listener | (outcomes, ranks) => void |
Returns
() => void
onSaid()?
onSaid?(listener): () => void;Defined in: packages/colyseus/src/index.ts:79
Agent replies per team (chat mode). Optional so older match implementations still fit.
Parameters
| Parameter | Type |
|---|---|
listener | (team, said) => void |
Returns
() => void
onView()
onView(team, listener): () => void;Defined in: packages/colyseus/src/index.ts:74
Subscribe to a team's view stream (computed only while subscribed); notified with each new version.
Parameters
| Parameter | Type |
|---|---|
team | string |
listener | (version) => void |
Returns
() => void
outcome()
outcome(team): Outcome;Defined in: packages/colyseus/src/index.ts:70
Parameters
| Parameter | Type |
|---|---|
team | string |
Returns
readiness()
readiness(): ReadinessSnapshot;Defined in: packages/colyseus/src/index.ts:68
Returns
release()
release(push): void;Defined in: packages/colyseus/src/index.ts:81
Parameters
| Parameter | Type |
|---|---|
push | (msg) => void |
Returns
void
report()
report(team): unknown;Defined in: packages/colyseus/src/index.ts:71
Parameters
| Parameter | Type |
|---|---|
team | string |
Returns
unknown
seats()
seats(): SeatInfo[];Defined in: packages/colyseus/src/index.ts:65
Returns
SeatInfo[]
setHumanReady()
setHumanReady(
team,
role,
ready
): void;Defined in: packages/colyseus/src/index.ts:69
Parameters
| Parameter | Type |
|---|---|
team | string |
role | string |
ready | boolean |
Returns
void
viewMessages()
viewMessages(team, after?): ViewMessage[];Defined in: packages/colyseus/src/index.ts:76
Messages bringing a reader at version after (undefined: fresh) up to date: missed deltas or one keyframe.
Parameters
| Parameter | Type |
|---|---|
team | string |
after? | number |
Returns
PlayerThreeRoomBinding
Defined in: packages/colyseus/src/index.ts:131
The room-side handle: call these from the room's lifecycle hooks.
Methods
dispose()
dispose(): void;Defined in: packages/colyseus/src/index.ts:141
Call from Room#onDispose. Idempotent.
Returns
void
onAuth()
onAuth(client, options): false | Principal;Defined in: packages/colyseus/src/index.ts:133
Call from Room#onAuth. Returns the principal (becomes client.auth) or false to reject the join.
Parameters
| Parameter | Type |
|---|---|
client | Client |
options | unknown |
Returns
false | Principal
onJoin()
onJoin(client, options?): void;Defined in: packages/colyseus/src/index.ts:135
Call from Room#onJoin. Human view: false suppresses views/reports; spectators cannot expand their scope.
Parameters
| Parameter | Type |
|---|---|
client | Client |
options? | unknown |
Returns
void
onLeave()
onLeave(client): void;Defined in: packages/colyseus/src/index.ts:137
Call from Room#onLeave (and onDrop if you do not allow reconnection).
Parameters
| Parameter | Type |
|---|---|
client | Client |
Returns
void
principal()
principal(client): Principal | undefined;Defined in: packages/colyseus/src/index.ts:139
Principal of a joined client.
Parameters
| Parameter | Type |
|---|---|
client | Client |
Returns
Principal | undefined
PlayerThreeRoomLike
Defined in: packages/colyseus/src/index.ts:125
The parts of a Colyseus Room the binding uses. Structural, so rooms from another copy of @colyseus/core
(e.g. a linked workspace) are accepted.
Properties
| Property | Modifier | Type | Defined in |
|---|---|---|---|
clock | readonly | object | packages/colyseus/src/index.ts:127 |
clock.setInterval | public | (handler, ms) => object | packages/colyseus/src/index.ts:127 |
Methods
onMessage()
onMessage(type, callback): unknown;Defined in: packages/colyseus/src/index.ts:126
Parameters
| Parameter | Type |
|---|---|
type | string |
callback | (client, message) => unknown |
Returns
unknown
PlayerThreeRoomOptions
Defined in: packages/colyseus/src/index.ts:95
Options of bindPlayerThreeRoom.
Properties
| Property | Type | Description | Defined in |
|---|---|---|---|
allow? | readonly ("observer" | "team" | "human" | "seat" | "spectator")[] | Principal kinds allowed to join this room. Default: all declared principal kinds. | packages/colyseus/src/index.ts:110 |
disposeMatch? | boolean | Dispose the PlayerThreeMatch together with the room. Default true. | packages/colyseus/src/index.ts:112 |
host | PlayerThreeHostPort | - | packages/colyseus/src/index.ts:96 |
match | PlayerThreeMatchPort | The match this room carries. Tokens of any other match are rejected. | packages/colyseus/src/index.ts:98 |
maxInFlight? | number | Unanswered control-plane requests allowed per client before replying busy. Default 4. | packages/colyseus/src/index.ts:106 |
maxMessageBytes? | number | Inbound message cap in bytes (JSON-encoded size), default MAX_MESSAGE_BYTES. The WebSocket transport has its own frame cap (WebSocketTransport({ maxPayload }), 4 KiB by default) — raise it to at least this value if agents deploy policies over the room. | packages/colyseus/src/index.ts:104 |
seatsIntervalMs? | number | How often seat changes (liveness, policy versions) are checked, ms. Default 1000. | packages/colyseus/src/index.ts:108 |
viewIntervalMs? | number | Minimum time between two batches of views to one client, ms (views in between are coalesced: the latest per team wins). Default 50 (≤ 20 per second); 0 = no rate bound, only the per-client ping/pong flow control. The room clock retries pending views even when the match is paused. | packages/colyseus/src/index.ts:118 |
Functions
bindPlayerThreeRoom()
function bindPlayerThreeRoom(room, opts): PlayerThreeRoomBinding;Defined in: packages/colyseus/src/index.ts:227
Binds room to opts.match: registers the PlayerThree message handlers and returns the lifecycle hooks to call (see the module example).
Parameters
| Parameter | Type |
|---|---|
room | PlayerThreeRoomLike |
opts | PlayerThreeRoomOptions |
