RoomDefinition

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

The RoomDefinition interface of the Room API is a room’s code: the object passed to defineRoom(), which a room module exports as its default. It holds the room’s settings, such as its schema and tick rate, and the hooks the room host calls as the room runs.

Every member is optional. A room module is one room type, and every room of that type runs the same definition. The room host calls each hook with the Room it’s running for.

A definition is either a room with ticks, which has a tickRate, or a room without them. Only a room with ticks may have stepper, history and onTick; see With or without ticks.

Instance properties

RoomDefinition.schema
The room’s Schema, shared with its clients. Without one, the room has no entities or calls.
RoomDefinition.tickRate
Ticks per second, an integer from 1 to 120. Without one, the room has no ticks.
RoomDefinition.stepper
The simulation step the room and its clients share. Needs a tickRate.
RoomDefinition.interest
Decides which entities each connection sees. Without it, every connection sees every entity.
RoomDefinition.voice
Turns on proximity voice and decides who hears whom.
RoomDefinition.history
The fields the room records each tick for room.rewind(). Needs a tickRate.
RoomDefinition.data
Makes room.data each time the room starts or wakes.
RoomDefinition.version
The room state version. Defaults to 1.
RoomDefinition.migrations
Up migrations of room state, keyed by the version each one produces.
RoomDefinition.privateRetention
How long a player’s private state is kept after the player was last connected. Defaults to 24 hours.
RoomDefinition.sleep
Puts the room to sleep once it has been quiet for a time, with its players still connected.
RoomDefinition.bots
The behaviours the room’s bots run, by name.

Hooks

onCreate()
Runs once, when the room starts with no room state.
onWake()
Runs when the room starts from room state, after hibernating or after its room host restarted.
onAlarm()
Runs when the room’s alarm is due, whether the room is active or hibernated.
onJoin()
Runs when a connection opens, including each reconnect.
seesPresence()
Decides whether one connection sees another player’s presence entry.
onMessage()
Runs for each message from a connection.
onLeave()
Runs when a connection closes, and for each open connection before the room hibernates.
onPlayerLeave()
Runs once when a player’s presence ends.
onTick()
Runs each tick, after the stepper. Needs a tickRate.
onHibernate()
Runs before the room saves its room state and leaves its room host.
onRequest()
Answers a message from the server API.
calls
A handler for each call the schema declares.
writes
A handler for each kind with writable fields, which decides what an owner write applies.

When a hook throws

What happens depends on the hook. A hook’s promise that rejects counts as a throw.

Hook If it throws
onCreate, onWake, onTick, onHibernate The room stops.
onAlarm It runs again after 2, 4, 8, 16, 32 and 64 seconds, then the alarm is dropped. The error is logged and the room keeps running.
onJoin That connection closes with close code 4006. The room keeps running.
onMessage The error is logged with the room, player and tick, and the message is dropped. The room keeps running.
onLeave, onPlayerLeave The error is logged. The room keeps running.
onRequest The server API call gets a 422 room_error reply with the error’s message. The error is logged unless it’s a CallRejected.
calls[name] The client’s call rejects with the error’s message. The error is logged unless it’s a CallRejected. The room keeps running.
writes[kind] The error is logged, the write is rejected and the owner corrected.

A room that stops reports the error and closes its connections with close code 4007. Other rooms in the same room host keep running. An error in the stepper, in an interest function, in seesPresence or in a migration also stops the room.

onCreate, onWake and onHibernate may be async: the room starts, or hibernates, once their promise resolves. onRequest and call handlers may be async too, and their reply is what the promise resolves to. The other hooks may return a promise, but the room doesn’t wait for it.

Order within a tick

In a room with ticks, each tick:

  1. Applies owner writes through writes.
  2. Runs the stepper with this tick’s inputs.
  3. Runs onTick.
  4. Sends the state update to every connection.
  5. Acknowledges the inputs it applied.

Messages and calls are handled as they arrive, between ticks, and their changes go out with the next tick’s state update. A connection’s owner writes that came before its message or call are applied first, through writes, so the handler sees what the player wrote; other connections’ writes wait for the tick.

In a room without ticks, everything room code changes goes out in one state update once the code yields. Each connection’s owner writes are applied through writes as they arrive.

Work timed by the clock rather than the tick runs in every room: a player’s presence grace ending, with onPlayerLeave, onAlarm, voice mutes expiring, adding bots up to a fill level, and sleep. In a room with ticks, what it changes goes out with the next tick.

Examples

A small arena

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

const Ship = entity("ship", { owner: "string", x: "f32", y: "f32" }, { key: "owner" });

export default defineRoom({
  schema: defineSchema({ entities: [Ship] }),
  tickRate: 20,
  onJoin(room, connection) {
    if (!room.entities.get(Ship, connection.player)) room.entities.create(Ship, { owner: connection.player });
  },
  onTick(room, dt) {
    for (const ship of room.entities.all(Ship)) ship.x = (ship.x + 4 * dt) % 100;
  },
  onPlayerLeave(room, player) {
    const ship = room.entities.get(Ship, player);
    if (ship) room.entities.delete(ship);
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
RoomDefinition0.10.10.10.1