RoomClient: update event
Available since
@irtio/client 5.0BrowsersNodeThe update event of the RoomClient interface fires when the client has applied a whole state update from the room.
Syntax
addEventListener("update", (event) => {})
Event type
A CustomEvent whose detail is an object with these properties:
update- A number. The state update’s number.
tickOptional- A number. The tick the state update is from, in a room with ticks. Absent in a room without ticks.
Description
The room sends a state update only when something this client sees changed, so a room where nothing changes fires no update. For the room’s current tick, use serverTick(), not the last update’s tick.
update fires after the update’s entity events, such as onEnter() and onChange() callbacks, have run. The full state update that ends a connect or reconnect fires an open event, or a wake event after a sleep, then update.
Examples
Redrawing a scoreboard
A scoreboard redraws once per state update rather than once per changed field.
// scoreboard.ts
import { connect, defineSchema, entity } from "@irtio/client";
const Match = entity("match", { red: "u16", blue: "u16" });
const schema = defineSchema({ state: Match });
const room = await connect("https://rooms.irt.io/my-game/final", { schema });
const score = document.querySelector("#score")!;
room.addEventListener("update", (event) => {
score.textContent = `${room.state.red} : ${room.state.blue}`;
if (event.detail.tick !== undefined) score.setAttribute("data-tick", `${event.detail.tick}`);
});
See also
EntityStore, for callbacks per entityRoomClient.serverTick()- State sync, for what the client receives
Runtime availability
| Browsers | Node | |
|---|---|---|
update event | 5.0 | 5.0 |