RoomClient: lobby event
@irtio/client 5.0BrowsersNodeThe lobby event of the RoomClient interface fires when the state of the lobby the client is in changes, and sets lobby.
Syntax
addEventListener("lobby", (event) => {})
Event type
A CustomEvent whose detail is the new room.lobby, an object with these properties:
phase- A string:
"waiting","countdown"or"handing-off". ready- An array of player IDs: the ready people in the order they joined, then every bot, which is always ready.
startsAt- A number or
null. When the countdown ends, in the room’s wall-clock milliseconds, so compare it withserverTime().nulloutside the countdown. min- A number. The lobby’s
minPlayers. max- A number. The lobby’s
maxPlayers. failed- A string or
null. Why the last handoff failed, until the next countdown:"players"when too few people were left,"limit"for a room limit,"spending"for a spending limit,"unavailable"for anything else.nullwhen no handoff failed.
Description
A lobby sends its state after the client’s first state update and whenever it changes, so lobby fires once the client has joined and again on every change. It never fires in a room that isn’t a lobby.
The event fires on changes only, so a countdown display needs its own timer between them. setLobbyReady(), startCountdown() and cancelCountdown() change the lobby’s state, and the change arrives as a lobby event.
Examples
Showing a lobby’s progress
A party game’s lobby screen shows how many are ready, the countdown and the last failure.
// lobby-screen.ts
import { connect } from "@irtio/client";
const room = await connect("https://rooms.irt.io/my-game/lobby", { type: "lobby" });
const status = document.querySelector("#lobby-status")!;
room.addEventListener("lobby", ({ detail: lobby }) => {
if (lobby.failed) console.warn(`the last start failed: ${lobby.failed}`);
if (lobby.phase === "waiting") status.textContent = `${lobby.ready.length} ready, ${lobby.min} needed`;
if (lobby.phase === "countdown" && lobby.startsAt !== null) {
const seconds = Math.ceil((lobby.startsAt - (room.serverTime() ?? Date.now())) / 1000);
status.textContent = `Starting in ${seconds} s`;
}
if (lobby.phase === "handing-off") status.textContent = "Finding a match";
});
serverTime() is undefined before the client’s first ping is answered, so the example falls back to the local clock.
See also
Runtime availability
| Browsers | Node | |
|---|---|---|
lobby event | 5.0 | 5.0 |