# Room

> The Room interface of the Room API is one running room, passed to every hook.

The **`Room`** interface of the [Room API](/docs/reference/room-api) is one running room: who is connected to it, the state it keeps, and the methods that send messages, remove players and schedule work. Every hook of a room definition receives it as its first argument.

Room code never constructs a `Room`. The room host makes one when the room is created or wakes.

> [!NOTE]
> `Room<S, P, D>` takes the room's kinds, not its schema. `RoomOf<typeof schema>` is the same type from the schema itself; see [Room SDK](/docs/reference/room-sdk#room).

## Instance properties

`Room.id`
: The room ID. Read only.

[`Room.tick`](/docs/reference/room-api/room/tick)
: The number of ticks the room has run, kept across hibernation. Read only.

`Room.tickRate`
: Ticks per second. Read only.

`Room.connections`
: A read-only `Map` of the open connections, by connection ID.

`Room.state`
: The shared state every connection sees.

`Room.data`
: An object for the room's own use. It isn't synced and isn't saved in room state.

## Instance methods

[`Room.broadcast()`](/docs/reference/room-api/room/broadcast)
: Sends a message to every connection.

[`Room.send()`](/docs/reference/room-api/room/send)
: Sends a message to one connection.

`Room.kick()`
: Closes a connection with close code 4006.

`Room.now()`
: Returns the wall-clock time in milliseconds. Under `@irtio/testing` it follows the test clock, unlike `Date.now()`.

`Room.setAlarm()`
: Sets the time at which `onAlarm` runs, whether the room is active or hibernated then.

`Room.requestHibernate()`
: Asks for the room to hibernate.

## Hooks

Hooks are members of the room definition, not of `Room`, and each is called with the `Room`.

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

## Examples

### Greeting a player

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

export default defineRoom({
  onJoin(room, connection) {
    room.send(connection, { type: "welcome", tick: room.tick });
    room.broadcast({ type: "joined", player: connection.player }, { except: connection });
  },
});
```

## See also

- [Room SDK](/docs/reference/room-sdk#room), for the members that don't have a page yet
- [State sync](/docs/guides/state-sync)
