# Room API

> The interfaces, functions and hooks room code is written with, one page each.

> [!NOTE]
> This reference is being split into a page per interface and member. Until every member has its own page, the [Room SDK](/docs/reference/room-sdk) page lists all of them.

The **Room API** is what room code uses to run one game instance: it receives players as they connect, exchanges messages with them, keeps state that is synced to every client, and decides when the room hibernates. It's exported by `@irtio/room`.

A room module's default export is the result of `defineRoom()`. The hooks of that definition each receive a [`Room`](/docs/reference/room-api/room), the object the rest of the API hangs from.

## Concepts and usage

Room code doesn't open sockets or run a loop. It exports a definition, and the room host calls its hooks: when the room starts, when a connection opens or closes, when a message arrives, and on every tick. Inside a hook, the `Room` reaches everyone connected and the room's state.

A room that nobody is connected to hibernates, and the next connection wakes it. Whatever is kept in room state is there again after the wake; see [How rooms hibernate and wake](/docs/hibernation).

## Interfaces

[`Room`](/docs/reference/room-api/room)
: One running room: its connections, its state, and the methods that send messages, remove players and schedule work.

## Hooks

[`onJoin()`](/docs/reference/room-api/onjoin)
: Runs when a connection opens, including each reconnect.

## Examples

### Relaying messages

This room sends every message it receives to everyone else in the room.

```ts
// relay.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  onMessage(room, connection, message) {
    room.broadcast(message, { except: connection });
  },
});
```

## See also

- [Room SDK](/docs/reference/room-sdk), the whole of `@irtio/room` on one page
- [Client SDK](/docs/reference/client-sdk), the other end of every connection
- [State sync](/docs/guides/state-sync)
- [Calls](/docs/guides/calls)
