RoomClient: addEventListener() method

Available since @irtio/client 5.0BrowsersNode

The addEventListener() method of the RoomClient interface adds a listener for one of the client’s events, as it does on any EventTarget. The listener’s event is typed for the event it listens to, with no cast.

Syntax

addEventListener(type, listener)
addEventListener(type, listener, options)

Parameters

type

A string, the event’s name. It’s one of:

waking event
The room isn’t running, and the connection is held while it starts.
open event
The client holds the room’s full state after having no connection.
reconnecting event
The connection dropped and the client is reconnecting.
close event
The connection ended for good.
message event
A message from the room arrived.
update event
A whole state update was applied.
sleep event
The room went to sleep, and the router holds the connection.
wake event
The room woke from sleep.
peers event
Who this player hears, or is heard by, changed.
voice event
The router granted voice for this connection.
lobby event
A lobby’s state changed.
handoff event
A handoff connected the player to another room, or failed to.
listener
A function that receives the event, or an object with a handleEvent() method that does. In a function, this is the client. null adds nothing.
options Optional

A boolean, which sets capture, or an object with any of these properties:

once
A boolean. If true, the listener is removed after it runs once. Defaults to false.
signal
An AbortSignal. Aborting it removes the listener.
capture and passive
Accepted as on any EventTarget. The client’s events don’t bubble and can’t be cancelled, so neither changes anything.

Return value

None (undefined).

Exceptions

None.

Description

The events are typed by RoomClientEventMap, as WebSocket’s are in the DOM’s types: a message listener gets a RoomMessageEvent, and a close listener a RoomCloseEvent.

A room’s messages can reach the client before connect() resolves, such as those it sends in onJoin(). The client holds reliable messages that arrive before the first message listener is added, up to 100, and gives them to the listeners once the code that added the first one has run, so listeners added together all get them. Held messages come first, in the order the room sent them, each after the state updates that arrived with it are applied. Unreliable messages aren’t held: before the first listener, they’re dropped. Once the first message listener has been added, nothing is held, so a listener added later gets only messages from then on.

waking fires before connect() resolves, and the first open with it. To listen for them, construct the client with new RoomClient, add the listeners, then await its ready.

To remove a listener, call removeEventListener() with the same function, or abort its signal. One AbortController can remove a scene’s listeners together, including the callbacks of room.entities and room.presence, which take the same signal option.

Examples

Getting the hand dealt on joining

The room deals each player a hand in onJoin(), before connect() resolves. The listener added after await connect() still gets it, since the client held it.

// hand.ts
import { connect } from "@irtio/client";

const room = await connect("table-3");

room.addEventListener("message", ({ data }) => {
  const m = data as { type?: string; cards?: string[] };
  if (m.type === "hand") console.log(`Dealt ${m.cards?.join(", ")}`);
});

Removing a scene’s listeners together

A scoreboard scene adds its listeners with one signal and removes them all when the player leaves the scene.

// scoreboard.ts
import type { ReadyRoomClient } from "@irtio/client";

export function showScoreboard(room: ReadyRoomClient): () => void {
  const scene = new AbortController();
  const { signal } = scene;
  room.addEventListener("update", ({ detail }) => console.log(`Scores as of update ${detail.update}`), { signal });
  room.addEventListener("reconnecting", ({ code }) => console.warn(`Reconnecting after ${code}`), { signal });
  room.presence.addEventListener("join", ({ entry }) => console.log(`${entry.name} joined`), { signal });
  return () => scene.abort();
}

See also

Runtime availability

BrowsersNode
addEventListener()5.05.0