# RoomClient

> The RoomClient class of the Client API is one client's connection to a room, with the room's state, its events and the methods that send messages and calls.

The **`RoomClient`** class of the [Client API](/docs/client) 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()`](/docs/client/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

```ts
new RoomClient(input)
new RoomClient(input, init)
```

`input`
: A string or a `URL`: the room's URL, absolute or relative to the project URL.

`init` Optional
: An object with the options `connect()` takes; see [`connect()`](/docs/client/connect).

The constructor starts connecting, as `connect()` does, and returns at once. Await [`ready`](/docs/client/room-client/ready) for the client `connect()` would resolve to. Construct the client yourself when a listener must see the [`waking` event](/docs/client/room-client/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`](/docs/client/irt-error) in a runtime without a global `WebSocket`, such as Node before version 24.

## Instance properties

All are read only.

[`RoomClient.ready`](/docs/client/room-client/ready)
: A `Promise` that resolves to the client once it holds the room's full state.

[`RoomClient.readyState`](/docs/client/room-client/readystate)
: Where the connection is: `"connecting"`, `"waking"`, `"open"`, `"reconnecting"` or `"closed"`.

[`RoomClient.url`](/docs/client/room-client/url)
: The room's URL, absolute.

[`RoomClient.id`](/docs/client/room-client/id)
: The room ID, the last segment of its URL.

[`RoomClient.project`](/docs/client/room-client/project)
: The project URL the room's URL resolves against.

[`RoomClient.type`](/docs/client/room-client/type)
: The room type the client expects.

[`RoomClient.schema`](/docs/client/room-client/schema)
: The schema the client was given, shared with the room.

[`RoomClient.player`](/docs/client/room-client/player)
: The player ID the client connected as.

[`RoomClient.connection`](/docs/client/room-client/connection)
: The ID of the current connection.

[`RoomClient.state`](/docs/client/room-client/state)
: The room's shared state, set once the client is ready.

[`RoomClient.private`](/docs/client/room-client/private)
: The player's private state, set once the client is ready.

[`RoomClient.entities`](/docs/client/room-client/entities)
: Every entity the client holds, with callbacks for their changes.

[`RoomClient.presence`](/docs/client/room-client/presence)
: Who is in the room.

[`RoomClient.calls`](/docs/client/room-client/calls)
: A typed async function for each call the schema declares.

[`RoomClient.generation`](/docs/client/room-client/generation)
: The room's generation, which changes when the room restarts or wakes.

[`RoomClient.tickRate`](/docs/client/room-client/tickrate)
: The room's ticks per second, or 0 in a room without ticks.

[`RoomClient.tick`](/docs/client/room-client/tick)
: The tick of the last state update the client applied.

[`RoomClient.update`](/docs/client/room-client/update)
: The update number of the last state update the client applied.

[`RoomClient.rtt`](/docs/client/room-client/rtt)
: The smallest recent round trip to the room, in milliseconds.

[`RoomClient.jitter`](/docs/client/room-client/jitter)
: How much the recent round trips vary, in milliseconds.

[`RoomClient.transportKind`](/docs/client/room-client/transportkind)
: What carries the connection: `"webtransport"` or `"websocket"`.

[`RoomClient.joinTime`](/docs/client/room-client/jointime)
: How long joining took, and whether the room started on the way.

[`RoomClient.protocol`](/docs/client/room-client/protocol)
: The client protocol the connection speaks.

[`RoomClient.asleep`](/docs/client/room-client/asleep)
: Whether the room sleeps while the router holds the connection.

[`RoomClient.lobby`](/docs/client/room-client/lobby)
: A lobby's phase, who is ready and the countdown, or `undefined` in a room that isn't a lobby.

[`RoomClient.peers`](/docs/client/room-client/peers)
: The players this player hears and is heard by in voice chat.

[`RoomClient.voice`](/docs/client/room-client/voice)
: The voice server's URL and this connection's token, when the project uses voice.

[`RoomClient.stats`](/docs/client/room-client/stats)
: The messages and bytes the client sent and received.

[`RoomClient.predictions`](/docs/client/room-client/predictions)
: Every prediction `predict()` made on the client.

[`RoomClient.playerData`](/docs/client/room-client/playerdata)
: The player's data, kept across every room of the project.

## Instance methods

[`RoomClient.send()`](/docs/client/room-client/send)
: Sends a message to the room.

[`RoomClient.flush()`](/docs/client/room-client/flush)
: Sends the player's unsent owner writes now.

[`RoomClient.close()`](/docs/client/room-client/close)
: Closes the connection for good, as the player leaving.

[`RoomClient.serverTick()`](/docs/client/room-client/servertick)
: Returns the room's current tick, estimated, with a fraction.

[`RoomClient.serverTime()`](/docs/client/room-client/servertime)
: Returns the room's wall clock, estimated, in milliseconds.

[`RoomClient.predict()`](/docs/client/room-client/predict)
: Predicts an entity the player controls with the room's stepper.

[`RoomClient.interpolate()`](/docs/client/room-client/interpolate)
: Draws remote entities of a kind between state updates.

[`RoomClient.setLobbyReady()`](/docs/client/room-client/setlobbyready)
: Marks the player ready or not in a lobby.

[`RoomClient.startCountdown()`](/docs/client/room-client/startcountdown)
: Starts a lobby's countdown, for the lobby's leader.

[`RoomClient.cancelCountdown()`](/docs/client/room-client/cancelcountdown)
: Cancels a lobby's countdown, for the lobby's leader.

[`RoomClient.collection()`](/docs/client/room-client/collection)
: Returns one of the project's collections.

[`RoomClient.leaderboard()`](/docs/client/room-client/leaderboard)
: Returns one of the project's leaderboards.

[`RoomClient.addEventListener()`](/docs/client/room-client/addeventlistener)
: Adds a listener for one of the client's events, typed so that it gets its event's class.

[`RoomClient.removeEventListener()`](/docs/client/room-client/removeeventlistener)
: Removes a listener that `addEventListener()` added.

## Events

Listen to these events with `addEventListener()`.

[`waking` event](/docs/client/room-client/waking-event)
: The room isn't running, and the connection is held while it starts.

[`open` event](/docs/client/room-client/open-event)
: The client holds the room's full state after having no connection.

[`reconnecting` event](/docs/client/room-client/reconnecting-event)
: The connection dropped and the client is reconnecting.

[`close` event](/docs/client/room-client/close-event)
: The connection ended for good.

[`message` event](/docs/client/room-client/message-event)
: A message from the room arrived.

[`update` event](/docs/client/room-client/update-event)
: A whole state update was applied.

[`sleep` event](/docs/client/room-client/sleep-event)
: The room went to sleep, and the router holds the connection.

[`wake` event](/docs/client/room-client/wake-event)
: The room woke from sleep, and the client holds its full state again.

[`peers` event](/docs/client/room-client/peers-event)
: Who the player hears or is heard by in voice chat changed.

[`voice` event](/docs/client/room-client/voice-event)
: The router granted voice for the connection.

[`lobby` event](/docs/client/room-client/lobby-event)
: A lobby's state changed.

[`handoff` event](/docs/client/room-client/handoff-event)
: 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.

```ts
// 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()`](/docs/client/connect)
- [Client API on one page](/docs/client/one-page#roomclient), the whole of `@irtio/client` on one page
- [`Room`](/docs/room/room), the room's side of the connection
- [Errors and close codes](/docs/client/errors)
