# RoomClient: addEventListener() method

> The addEventListener() method of the RoomClient interface adds a listener for one of the client's events.

The **`addEventListener()`** method of the [`RoomClient`](/docs/client/room-client) 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

```ts
addEventListener(type, listener)
addEventListener(type, listener, options)
```

### Parameters

`type`
: A string, the event's name. It's one of:

  [`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.

  [`peers` event](/docs/client/room-client/peers-event)
  : Who this player hears, or is heard by, changed.

  [`voice` event](/docs/client/room-client/voice-event)
  : The router granted voice for this 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.

`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`](/docs/client/room-message-event), and a `close` listener a [`RoomCloseEvent`](/docs/client/room-close-event).

A room's messages can reach the client before [`connect()`](/docs/client/connect) resolves, such as those it sends in [`onJoin()`](/docs/room/room-definition/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`](/docs/client/room-client/ready).

To remove a listener, call [`removeEventListener()`](/docs/client/room-client/removeeventlistener) with the same function, or abort its `signal`. One `AbortController` can remove a scene's listeners together, including the callbacks of [`room.entities`](/docs/client/room-client/entities) and [`room.presence`](/docs/client/room-client/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.

```ts
// 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.

```ts
// 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

- [`RoomClient.removeEventListener()`](/docs/client/room-client/removeeventlistener)
- [`message` event](/docs/client/room-client/message-event)
- [`close` event](/docs/client/room-client/close-event)
- [Errors and close codes](/docs/client/errors)
