RoomDefinition: onWake() hook

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

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

Syntax

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

Parameters

room
The 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 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() 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.

Around onWake:

  • A stepper’s load runs before it.
  • Each bot’s start runs after it.
  • With reason "alarm", onAlarm() runs straight after it.
  • With slept true, each connection is back with a new ID and its data, without onJoin. After onWake, 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.

// 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

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
onWake()0.10.10.10.1