# RoomDefinition: sleep property

> The sleep property of a room definition puts the room to sleep once it has been quiet for a time, with its players still connected.

The **`sleep`** property of a [room definition](/docs/room/room-definition) puts the room to sleep once it has gone quiet: it hibernates while the router holds its players' connections, and the next thing a player does wakes it, with no one leaving. Time asleep costs no room-hours.

## Value

An object with this property:

`after`
: A positive number of milliseconds. The room sleeps once it has been quiet this long.

Without `sleep`, a room sleeps only when its code calls `room.sleep()`.

## Exceptions

`defineRoom()` throws a `RangeError` for a `sleep.after` that isn't a positive number of milliseconds.

## Description

A room is quiet while no client sends a frame other than a ping, the room sends, broadcasts and stores nothing, no call or server API request is in flight, and no player's presence is in its grace. The room host refuses to put a room to sleep while its alarm is due within 2 minutes; the room asks again after its next quiet period.

Opting in promises that the room keeps every deadline in its alarm, not in `setTimeout` or by counting ticks, and keeps in room state, or rebuilds in [`onWake()`](/docs/room/room-definition/onwake), anything else it needs.

When the room goes to sleep:

- [`onLeave()`](/docs/room/room-definition/onleave) and [`onPlayerLeave()`](/docs/room/room-definition/onplayerleave) don't run. [`onHibernate()`](/docs/room/room-definition/onhibernate) does, after the owner writes the room holds are applied through [`writes`](/docs/room/room-definition/writes).
- Presence is kept, and each connection with its `data` as JSON of at most `MAX_SLEPT_DATA`, 4 KB. A connection's `data` over that, or that doesn't serialize, wakes as `{}`, and the room logs it.

A message, call, owner write or presence status from a held connection wakes it, and the router delivers it after the wake, in order. So does a held connection closing, a new connection, the alarm and a server API call. Unreliable messages and inputs are dropped while it sleeps.

At the wake, each connection comes back with a new ID and its `data`, without `onJoin`. `onWake` runs with `slept` `true`, then `onLeave` for saved connections that didn't come back, then what the held connections sent; then ticks resume and every client gets a full state update.

Every wake is billed at least 10 seconds. A project's sleeping rooms hold at most `limits.maxSleepingConnections` connections in a region, 10,000 by default; past it, a room that sleeps closes its connections with close code 4011. A held connection may send 64 messages or 256 KB before its room wakes; past it, it closes with close code 4006.

## Examples

### A board game that sleeps between moves

```ts
// board.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";

const Board = entity("board", { cells: "string", turn: "string" });

export default defineRoom({
  schema: defineSchema({ state: Board }),
  sleep: { after: 60_000 },
  onCreate(room) {
    room.state.cells = ".".repeat(9);
  },
  onJoin(room, connection) {
    if (room.state.turn === "") room.state.turn = connection.player;
  },
  onMessage(room, connection, message) {
    const cell = (message as { cell?: unknown }).cell;
    if (typeof cell !== "number" || room.state.turn !== connection.player || room.state.cells[cell] !== ".") return;
    room.state.cells = room.state.cells.slice(0, cell) + "x" + room.state.cells.slice(cell + 1);
  },
});
```

## See also

- [Sleeping rooms](/docs/room/sdk#sleeping-rooms)
- [Hibernation](/docs/room/guides/hibernation#sleeping-rooms)
