RoomClient: sleep event
Available since
@irtio/client 5.0BrowsersNodeThe sleep event of the RoomClient interface fires when the room goes to sleep: it hibernates while the router holds this client’s connection.
Syntax
addEventListener("sleep", (event) => {})
Event type
A plain Event, with no properties of its own.
Description
When sleep fires, asleep is true. The connection stays open, and nobody leaves the room.
While the room sleeps:
- Messages, calls and writes the client sends wake the room and reach it once it has woken. Calls made meanwhile are answered, not failed.
- Unreliable messages and inputs are dropped.
- The client pings every 15 seconds instead of every 2. The router answers without the room’s tick, so the answers don’t change
serverTick()orrtt.
The room wakes when anything wakes it, such as another player’s move or its alarm, and the client fires a wake event. If the connection drops while the room sleeps, asleep is false again and the client reconnects as after any drop.
Examples
Pausing a clock while the room sleeps
A chess client stops animating its move clock while the room sleeps, since nothing in the room counts time then.
// chess-clock.ts
import { connect } from "@irtio/client";
const room = await connect("https://rooms.irt.io/my-game/chess-12");
const clock = document.querySelector("#clock")!;
room.addEventListener("sleep", () => {
clock.classList.add("paused");
});
room.addEventListener("wake", () => {
clock.classList.remove("paused");
});
See also
RoomClient.asleepwakeeventRoom.hibernate(), the room side- Sleeping rooms, for when a room sleeps
Runtime availability
| Browsers | Node | |
|---|---|---|
sleep event | 5.0 | 5.0 |