RoomDefinition: onWake() hook
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe 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
Roomthat 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
savedAtto 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). slepttruewhen the room went to sleep, so its connections are back inroom.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.datais new: what the definition’sdatareturned, 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
loadruns before it. - Each bot’s
startruns after it. - With
reason"alarm",onAlarm()runs straight after it. - With
slepttrue, each connection is back with a new ID and itsdata, withoutonJoin. AfteronWake,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 rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
onWake() | 0.1 | 0.1 | 0.1 | 0.1 |