RoomClient
@irtio/client 5.0BrowsersNodeThe RoomClient class of the Client API is one client’s connection to a room: the room’s state as it syncs, the events that say what happened to the connection and the methods that send messages and calls. connect() resolves to one once it holds the room’s full state.
A RoomClient reconnects by itself when its connection drops, and keeps the same object through every reconnect. It extends EventTarget.
Constructor
new RoomClient(input)
new RoomClient(input, init)
input- A string or a
URL: the room’s URL, absolute or relative to the project URL. initOptional- An object with the options
connect()takes; seeconnect().
The constructor starts connecting, as connect() does, and returns at once. Await ready for the client connect() would resolve to. Construct the client yourself when a listener must see the waking event, which fires before connect() resolves.
It throws a TypeError for a relative URL with no project URL to resolve it against, for a URL whose path doesn’t end in a project and a room ID, and for a URL that isn’t http:, https:, ws: or wss:. It throws an IrtError in a runtime without a global WebSocket, such as Node before version 24.
Instance properties
All are read only.
RoomClient.ready- A
Promisethat resolves to the client once it holds the room’s full state. RoomClient.readyState- Where the connection is:
"connecting","waking","open","reconnecting"or"closed". RoomClient.url- The room’s URL, absolute.
RoomClient.id- The room ID, the last segment of its URL.
RoomClient.project- The project URL the room’s URL resolves against.
RoomClient.type- The room type the client expects.
RoomClient.schema- The schema the client was given, shared with the room.
RoomClient.player- The player ID the client connected as.
RoomClient.connection- The ID of the current connection.
RoomClient.state- The room’s shared state, set once the client is ready.
RoomClient.private- The player’s private state, set once the client is ready.
RoomClient.entities- Every entity the client holds, with callbacks for their changes.
RoomClient.presence- Who is in the room.
RoomClient.calls- A typed async function for each call the schema declares.
RoomClient.generation- The room’s generation, which changes when the room restarts or wakes.
RoomClient.tickRate- The room’s ticks per second, or 0 in a room without ticks.
RoomClient.tick- The tick of the last state update the client applied.
RoomClient.update- The update number of the last state update the client applied.
RoomClient.rtt- The smallest recent round trip to the room, in milliseconds.
RoomClient.jitter- How much the recent round trips vary, in milliseconds.
RoomClient.transportKind- What carries the connection:
"webtransport"or"websocket". RoomClient.joinTime- How long joining took, and whether the room started on the way.
RoomClient.protocol- The client protocol the connection speaks.
RoomClient.asleep- Whether the room sleeps while the router holds the connection.
RoomClient.lobby- A lobby’s phase, who is ready and the countdown, or
undefinedin a room that isn’t a lobby. RoomClient.peers- The players this player hears and is heard by in voice chat.
RoomClient.voice- The voice server’s URL and this connection’s token, when the project uses voice.
RoomClient.stats- The messages and bytes the client sent and received.
RoomClient.predictions- Every prediction
predict()made on the client. RoomClient.playerData- The player’s data, kept across every room of the project.
Instance methods
RoomClient.send()- Sends a message to the room.
RoomClient.flush()- Sends the player’s unsent owner writes now.
RoomClient.close()- Closes the connection for good, as the player leaving.
RoomClient.serverTick()- Returns the room’s current tick, estimated, with a fraction.
RoomClient.serverTime()- Returns the room’s wall clock, estimated, in milliseconds.
RoomClient.predict()- Predicts an entity the player controls with the room’s stepper.
RoomClient.interpolate()- Draws remote entities of a kind between state updates.
RoomClient.setLobbyReady()- Marks the player ready or not in a lobby.
RoomClient.startCountdown()- Starts a lobby’s countdown, for the lobby’s leader.
RoomClient.cancelCountdown()- Cancels a lobby’s countdown, for the lobby’s leader.
RoomClient.collection()- Returns one of the project’s collections.
RoomClient.leaderboard()- Returns one of the project’s leaderboards.
RoomClient.addEventListener()- Adds a listener for one of the client’s events, typed so that it gets its event’s class.
RoomClient.removeEventListener()- Removes a listener that
addEventListener()added.
Events
Listen to these events with addEventListener().
wakingevent- The room isn’t running, and the connection is held while it starts.
openevent- The client holds the room’s full state after having no connection.
reconnectingevent- The connection dropped and the client is reconnecting.
closeevent- The connection ended for good.
messageevent- A message from the room arrived.
updateevent- A whole state update was applied.
sleepevent- The room went to sleep, and the router holds the connection.
wakeevent- The room woke from sleep, and the client holds its full state again.
peersevent- Who the player hears or is heard by in voice chat changed.
voiceevent- The router granted voice for the connection.
lobbyevent- A lobby’s state changed.
handoffevent- A handoff connected the player to another room, or failed to.
Description
RoomClient<S, P, C> takes the kinds of the room’s shared and private state and its calls, which the schema passed to connect() gives it. Before the client is ready, state and private are typed as possibly undefined. connect() and ready resolve to a ReadyRoomClient<S, P, C>, the same object typed with both set.
Examples
Showing who is in the room
This client shows the number of players in the room and says when it lost the connection.
// lobby-status.ts
import { connect } from "@irtio/client";
const room = await connect("main");
const show = () => console.log(`${room.presence.size} in ${room.id}`);
show();
room.presence.addEventListener("join", show);
room.presence.addEventListener("leave", show);
room.addEventListener("reconnecting", () => console.log("connection lost, reconnecting"));
room.addEventListener("close", ({ error }) => console.error(`left the room: ${error?.message ?? "closed"}`));
See also
connect()- Client API on one page, the whole of
@irtio/clienton one page Room, the room’s side of the connection- Errors and close codes
Runtime availability
| Browsers | Node | |
|---|---|---|
RoomClient | 5.0 | 5.0 |