Multiplayer rooms for the web

IRT is a TypeScript library for running multiplayer rooms for web games: the Room API for the code that runs a room, the Client API for the browser, and a room host that runs them.

Request an invite

Write your game's server as plain TypeScript, without the sockets, loops and save code that usually come with it. The room host runs your room on a fixed tick, and its entities sync to every client with their types intact, so the browser reads the same shapes the room writes.

Rooms

A room is one running game instance, addressed by a room ID. Room code doesn't open sockets or run a loop: it exports a definition, and the room host calls its hooks when a connection opens or closes, when a message arrives and on every tick. Clients connect over WebTransport, with WebSocket as the fallback. Concepts defines each term.

  • Room API. What room code is written with: the room definition, its hooks, messages, typed entities and room state. Exported by @irtio/room.
  • Client API. What a browser or Node client uses to connect to a room, send messages and read its entities. Exported by @irtio/client.
  • Room host. The process that runs a build's rooms.

Room lifecycle

A room hibernates 30 seconds after its last connection closes, and the next connection wakes it with its room state as it was. Entities, room.state and private state are room state, saved and restored without any code of yours. A room that breaks is replaced; its players' connections are held. How rooms hibernate and wake specifies each step.

  1. 1

    Active

    Connections are open and the room runs its ticks.

  2. 2

    Hibernating

    The last connection closed 30 seconds ago. The room's onHibernate hook runs and its room state is written to the state store.

  3. 3

    Hibernated

    The room isn't running. Its room state is in the state store.

  4. 4

    Waking

    A client connects and the router holds the connection. The client SDK emits a wakingevent, and every connection held joins when the room has its state back.

Room and client code

The room and the client are TypeScript and import the schema from one shared module. Both examples are compiled against the packages by this site's tests.

A room declares typed entities and handles joins, messages and leaves. Each tick, the entities that changed are sent to clients as a binary delta, encoded once. The Room SDK reference lists every member.

shared.ts and room.ts
// shared.ts, imported by the room and the client
import { defineSchema, entity } from "@irtio/room";

export const Player = entity("player", {
  owner: "string", name: "string",
  x: "f32", z: "f32", hp: "u8",
});
export const schema = defineSchema({ entities: [Player] });

// room.ts
import { defineRoom, grid } from "@irtio/room";
import { Player, schema } from "./shared.js";

export default defineRoom({
  schema,
  interest: grid({ cellSize: 32, radius: 2, dims: "xz", around: (c) => c.data.me }),
  onJoin(room, conn) {
    const { name } = conn.meta as { name: string };
    conn.data.me = room.entities.create(Player, { owner: conn.player, name, hp: 100 });
  },
  onMessage(room, conn, msg) {
    const m = msg as { type?: string; text?: string };
    if (m.type === "chat") room.broadcast({ type: "chat", text: String(m.text), from: conn.player });
  },
  onLeave(room, conn) {
    room.entities.delete(conn.data.me);
  },
});

A client connects, interpolates remote entities and sends messages. Connecting to a hibernated room calls onWaking while the room wakes. The Client SDK reference lists every member.

client.ts
// client.ts
import { connect } from "@irtio/client";
import { Player, schema } from "./shared.js";

const room = await connect({
  router: "wss://eu.irt.io", project: "my-game", room: "forest-7", schema,
  onWaking: () => banner.show("Waking the room"),
});
room.on("restored", () => banner.hide());

const drawn = room.interpolate(Player, ["x", "z"]);
room.entities.onEnter(Player, (p) => scene.add(p.id, p.name));
room.entities.onLeave(Player, (p) => scene.remove(p.id));
scene.onFrame(() => {
  for (const p of room.entities.all(Player)) scene.move(p.id, drawn.get(p));
});

chat.onsubmit = (text: string) => room.send({ type: "chat", text });

What the library includes

Each of these is part of the packages and has its own page in the docs.

  • State sync. Interest keys, such as a grid cell or a team, decide which entities each connection sees.
  • Client prediction. The client runs the room's own step function to predict the local player.
  • Lag compensation. The room rewinds recent entity positions to check a shot against where the target was on the shooter's screen.
  • Physics. Rigid bodies and characters with Rapier 2D or 3D, stepped by the room and predicted on the client.
  • Proximity voice. Each listener hears the closest speakers it can see.
  • @irtio/testing. Rooms run in process in tests, with test clients and a controlled clock.
  • Games without a backend. Guest players, rooms that clients create, join codes and a list of public rooms are project settings.

Hosting on irt.io

irt.io is the official hosting for IRT. Hosting is metered in room-hours: the time a room is awake, at the size chosen for its room type. The number of players doesn't change it, and a room that has hibernated stops the meter. There are four meters.

  • Room-hours. One room awake for an hour, priced by room size.
  • Traffic. The bytes your rooms send to players and receive from them.
  • Voice-minutes. Per participant, per minute in a call. Only if you turn voice on.
  • Storage. The room state your hibernated rooms keep, and your replays and clips.

Getting started

The quickstart needs Node 24 or later. The irt CLI is @irtio/cli on npm, run as npx @irtio/cli.