# RoomDefinition: onWake() hook

> The onWake() hook of a room definition runs when the room starts from room state, after hibernating or after its room host restarted.

The **`onWake()`** hook of a [room definition](/docs/room/room-definition) runs when the room starts from room state: after it hibernated, or after its room host restarted.

## Syntax

```ts
defineRoom({
  onWake(room, info) {},
});
```

### Parameters

`room`
: The [`Room`](/docs/room/room) that woke.

`info`
: An object with these properties:

  `savedAt`
  : The wall-clock time in milliseconds that room state was last written: at the room's last tick with changes, or at its hibernation.

  `elapsed`
  : The milliseconds from `savedAt` to now, never negative.

  `reason`
  : What woke the room: `"connect"` (a connection), `"api"` (a server API call), `"alarm"` (its alarm was due) or `"message"` (a held connection of a sleeping room sent something or closed).

  `slept`
  : `true` when the room went to sleep, so its connections are back in `room.connections`.

### Return value

None, or a `Promise`. The room starts once `onWake` has run, or once the promise it returned resolves.

### Exceptions

If `onWake` throws, or its promise rejects, the room stops and its connections close with close code 4007.

## Description

Room state comes back as it was saved: entities, `room.state`, private state, the tick, the alarm, bans and bots. What wasn't room state doesn't, so `onWake` rebuilds it:

- `room.data` is new: what the definition's [`data`](/docs/room/room-definition/data) returned, or `{}`.
- A replay recording stopped at hibernation; start it again here.
- Presence is empty after an ordinary wake, apart from the room's bots, and players come back one [`onJoin()`](/docs/room/room-definition/onjoin) at a time.

Ticks stop while a room is hibernated, and none are run to catch up. A room that should progress while nobody is there computes it from `room.now()` or `elapsed`; see [Offline progress](/docs/room/guides/hibernation#offline-progress).

Around `onWake`:

- A stepper's `load` runs before it.
- Each bot's `start` runs after it.
- With `reason` `"alarm"`, [`onAlarm()`](/docs/room/room-definition/onalarm) runs straight after it.
- With `slept` `true`, each connection is back with a new ID and its `data`, without `onJoin`. After `onWake`, [`onLeave()`](/docs/room/room-definition/onleave) runs for each saved connection that didn't come back, then the room handles what the held connections sent.

## Examples

### Recording every session

A replay recording stops when the room hibernates, so a room that always records starts it in both `onCreate` and `onWake`.

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

export default defineRoom({
  tickRate: 20,
  onCreate(room) {
    room.record();
  },
  onWake(room, { elapsed, reason }) {
    room.record();
    console.log(`woke for ${reason} after ${Math.round(elapsed / 1000)} s`);
  },
});
```

## See also

- [`onCreate()`](/docs/room/room-definition/oncreate)
- [`onHibernate()`](/docs/room/room-definition/onhibernate)
- [Hibernation](/docs/room/guides/hibernation)
- [Sleeping rooms](/docs/room/sdk#sleeping-rooms)
