defineRoom() function

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

The defineRoom() function checks a room definition and returns it. A room module’s default export is its result.

Syntax

defineRoom(definition)

Parameters

definition
A RoomDefinition: the room’s schema, tick rate, stepper, interest and other settings, and its hooks.

Return value

The same definition, typed without its entity kinds, which only type its writes handlers.

Exceptions

RangeError
Thrown for a tickRate that isn’t an integer from 1 to 120, a sleep.after that isn’t a positive number of milliseconds, or a privateRetention that isn’t a positive number of milliseconds or "forever".
Error
Thrown for onTick, a stepper, a history or a bot behaviour with a tick hook in a room without a tickRate; for a history of a kind that isn’t one of the schema’s entity kinds; and for a writes key that isn’t the private kind or an entity kind with writable fields.

The types reject most of these too.

Examples

A turn-based room without ticks

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

const Game = entity("game", { turn: "string", moves: "u16" });

export default defineRoom({
  schema: defineSchema({ state: Game }),
  onJoin(room, connection) {
    if (room.state.turn === "") room.state.turn = connection.player;
  },
  onMessage(room, connection) {
    if (room.state.turn === connection.player) room.state.moves++;
  },
});

See also

Runtime availability

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