RoomDefinition: schema property

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

The schema property of a room definition is the room’s Schema, made with defineSchema(). It declares the room’s shared state, private state, entity kinds, calls and input, and the room’s clients connect with the same schema.

Value

A Schema. Without one, the room has no entities, shared or private state, or calls.

Description

The schema types the room: room.state, room.private(), the handlers calls must have and the keys writes may have all come from it.

Keep the schema in a module of its own that the room and the client both import. A client sends the schema’s hash when it connects, and a client built with a different schema fails to connect with close code 4004. The calls aren’t part of the hash.

A room module is one room type, with its own schema. A room never changes type, so a lobby type and a match type each have theirs.

Examples

A schema shared with the client

// shared.ts
import { call, defineCalls, defineSchema, entity } from "@irtio/room";

export const Table = entity("table", { pot: "u32", turn: "string" });
export const Hand = entity("hand", { chips: "u32" });

export const schema = defineSchema({
  state: Table,
  private: Hand,
  calls: defineCalls({ bet: call({ amount: "u32" }) }),
});
// room.ts
import { CallRejected, defineRoom } from "@irtio/room";
import { schema } from "./shared.js";

export default defineRoom({
  schema,
  calls: {
    bet({ amount }, { room, connection }) {
      const hand = room.private(connection);
      if (room.state.turn !== connection.player || amount > hand.chips) throw new CallRejected("you can't bet that");
      hand.chips -= amount;
      room.state.pot += amount;
    },
  },
});

See also

Runtime availability

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