RoomDefinition: data property

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

The data property of a room definition is a function that makes room.data, the room’s own object for what it derives or keeps in memory. It runs each time the room starts or wakes, before onCreate() or onWake(), and its return type types room.data.

Value

A function that takes no arguments and returns an object. Without it, room.data starts as {}.

Description

room.data is one plain object per room. It isn’t synced to clients and isn’t saved in room state, so it starts again from data() after every wake and after a room host restart. Build what it holds from room state in onCreate and again in onWake.

Use it, not a variable at the top level of the module, for anything that belongs to one room: a room host runs many rooms of one module, which share module variables. room.data can hold entity objects safely, since both are new objects after a wake.

Examples

Limiting chat per player

// slow-chat.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  data: () => ({ lastLine: new Map<string, number>() }),
  onMessage(room, connection, message) {
    if (typeof message !== "string") return;
    const now = room.now();
    if (now - (room.data.lastLine.get(connection.player) ?? 0) < 1000) return;
    room.data.lastLine.set(connection.player, now);
    room.broadcast({ from: connection.player, text: message.slice(0, 200) });
  },
});

See also

Runtime availability

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