RoomClient: state property
@irtio/client 5.0BrowsersNodeThe state read-only property of the RoomClient interface is the room’s shared state: the one entity of the schema’s state kind that every connection sees.
Value
A ClientEntity of the schema’s state kind, with its fields as read-only properties, or undefined until ready resolves. On the client that connect() and ready resolve to, it’s typed as set.
Exceptions
TypeError- Thrown if a field of
stateis written. The room owns the shared state; to change it, send a message or make a call.
Description
The client applies each state update whole, so the fields of state always come from one tick of the room. Shared state is entity 0, so entities.onChange() with the schema’s state kind runs when its fields change.
After a reconnect the room may send the shared state again as a new object. Read room.state each time rather than keeping the object.
Examples
Showing the round’s timer
The room keeps the round’s phase and the seconds left in shared state, and the client shows them after each update.
// timer.ts
import { connect, defineSchema, entity } from "@irtio/client";
const Round = entity("round", { phase: "string", secondsLeft: "u8" });
const room = await connect("main", { schema: defineSchema({ state: Round }) });
room.addEventListener("update", () => {
const { phase, secondsLeft } = room.state;
console.log(phase === "countdown" ? `starting in ${secondsLeft}` : phase);
});
See also
RoomClient.privateRoom.state, the room’s side- State sync
Runtime availability
| Browsers | Node | |
|---|---|---|
state | 5.0 | 5.0 |