# RoomClient: message event

> The message event of the RoomClient interface fires when a message from the room arrives.

The **`message`** event of the [`RoomClient`](/docs/client/room-client) interface fires when a message from the room arrives, one the room sent with [`room.send()`](/docs/room/room/send) or [`room.broadcast()`](/docs/room/room/broadcast).

## Syntax

```ts
addEventListener("message", (event) => {})
```

## Event type

A [`RoomMessageEvent`](/docs/client/room-message-event). It extends `Event` and adds these properties:

[`data`](/docs/client/room-message-event/data) Read only
: The message: what the room sent as JSON, parsed, or a `Uint8Array` for a binary message.

[`unreliable`](/docs/client/room-message-event/unreliable) Read only
: `true` when the room sent it unreliably.

[`from`](/docs/client/room-message-event#from) Read only
: `undefined` in a room with room code, whose messages all come from the room. In a native room, the sender's player ID, or `""` for a message sent through the server API.

## Description

### Messages before a listener

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. So `await connect(...)` then `room.addEventListener("message", ...)` gets every message the room sent on join.

Unreliable messages aren't held: before the first listener, they're dropped. After the first `message` listener has been added, nothing is held, so a listener added later gets only messages from then on.

### Order

Messages keep the order the room sent them in, held ones first. A held message is delivered after the state updates that arrived with it are applied. Unreliable messages can arrive out of order with reliable ones, and some never arrive.

## Examples

### Showing chat from the room

The room relays chat as JSON objects. The client checks each message's `type` before using it, since one room can send several kinds of message.

```ts
// chat-log.ts
import { connect } from "@irtio/client";

const room = await connect("https://rooms.irt.io/my-game/tavern");
const log = document.querySelector("#chat")!;

room.addEventListener("message", (event) => {
  if (event.data instanceof Uint8Array) return;
  // The room's messages aren't typed, so check the shape before using it
  const message = event.data as { type?: string; from?: string; text?: string };
  if (message.type !== "chat") return;
  const line = document.createElement("p");
  line.textContent = `${message.from}: ${message.text}`;
  log.append(line);
});
```

## See also

- [`RoomMessageEvent`](/docs/client/room-message-event)
- [`RoomClient.send()`](/docs/client/room-client/send), for messages to the room
- [`Room.broadcast()`](/docs/room/room/broadcast), the room side
- [Calls](/docs/room/guides/calls), for a request that needs a reply
