Room API

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

Note: This reference is being split into a page per interface and member. Until every member has its own page, the Room SDK page lists all of them.

The Room API is what room code uses to run one game instance: it receives players as they connect, exchanges messages with them, keeps state that is synced to every client, and decides when the room hibernates. It’s exported by @irtio/room.

A room module’s default export is the result of defineRoom(). The hooks of that definition each receive a Room, the object the rest of the API hangs from.

Concepts and usage

Room code doesn’t open sockets or run a loop. It exports a definition, and the room host calls its hooks: when the room starts, when a connection opens or closes, when a message arrives, and on every tick. Inside a hook, the Room reaches everyone connected and the room’s state.

A room that nobody is connected to hibernates, and the next connection wakes it. Whatever is kept in room state is there again after the wake; see How rooms hibernate and wake.

Interfaces

Room
One running room: its connections, its state, and the methods that send messages, remove players and schedule work.

Hooks

onJoin()
Runs when a connection opens, including each reconnect.

Examples

Relaying messages

This room sends every message it receives to everyone else in the room.

// relay.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  onMessage(room, connection, message) {
    room.broadcast(message, { except: connection });
  },
});

See also

Runtime availability

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