RoomClient: readyState property

Available since @irtio/client 5.0BrowsersNode

The readyState read-only property of the RoomClient interface says where the client’s connection is, as WebSocket.readyState does.

Value

A string, one of these:

"connecting"
The client is connecting for the first time. It’s the value when the client is constructed.
"waking"
The room isn’t running, and the router holds the connection while it starts. The client has fired a waking event.
"open"
The router accepted the connection. The first time, ready resolves when the full state update that follows is applied.
"reconnecting"
The connection dropped after the client had been ready, and the client is connecting again. The client has fired a reconnecting event.
"closed"
The connection ended for good, after close() or a close code the client doesn’t retry, or the first connection failed: it couldn’t be dialled, timeout passed or signal aborted. The client has fired the close event, and it never connects again.

Description

A connection that drops before the client was first ready goes back to "connecting", not "reconnecting". A room that sleeps while the router holds the connection stays "open"; asleep says it’s sleeping.

Examples

Labelling the connection in a debug overlay

A debug overlay turns each ready state into a label, and says when the room is asleep.

// overlay.ts
import type { ReadyState, RoomClient } from "@irtio/client";

const labels: Record<ReadyState, string> = {
  connecting: "Connecting",
  waking: "Starting the room",
  open: "Connected",
  reconnecting: "Reconnecting",
  closed: "Disconnected",
};

export function connectionLabel(room: RoomClient): string {
  return room.asleep ? "Connected, room asleep" : labels[room.readyState];
}

See also

Runtime availability

BrowsersNode
readyState5.05.0