RoomClient: ready property

Available since @irtio/client 5.0BrowsersNode

The ready read-only property of the RoomClient interface is a Promise that resolves to the client once it holds the room’s full state, when connect() would resolve.

Value

A Promise that resolves to the same client as a ReadyRoomClient, the client typed with state and private set.

It rejects with an IrtError when the client fails for good before it’s ready, as connect() rejects:

  • NetworkError when the room can’t be reached over WebTransport or WebSocket
  • an error named for the close code when the router closes the connection with a code the client doesn’t retry, such as VersionError for 4004
  • TimeoutError when the timeout option passes first, or the signal option comes from AbortSignal.timeout()
  • NotSupportedError when the runtime has no global WebSocket, as Node before 24
  • AbortError when another signal aborts, with its reason as cause, or when close() is called first

Description

ready settles once. It resolves when the client applies the room’s first full state update, and later reconnects don’t change it. After it resolves, the client reconnects by itself and fires an open event after each reconnect.

connect() is new RoomClient(input, init) followed by awaiting ready. Construct the client and await ready yourself when a listener must be added before the client connects, such as one for the waking event.

Examples

Showing that the room is starting

The player sees “Starting the room” while a hibernated room wakes, and an error if the client can’t connect.

// join-screen.ts
import { IrtError, RoomClient } from "@irtio/client";

const client = new RoomClient("main", { timeout: 20_000 });
client.addEventListener("waking", () => console.log("Starting the room"));
try {
  const room = await client.ready;
  console.log(`joined as ${room.player}`);
} catch (e) {
  if (!(e instanceof IrtError)) throw e;
  console.error(`couldn't join: ${e.name}`);
}

See also

Runtime availability

BrowsersNode
ready5.05.0