# Games without a backend

> Ship a game with no server of your own, with guest players, rooms clients create, join codes, quick join and a list of public rooms.

A game doesn't need a backend on IRT. Players join as **guests**, and
clients create rooms, share join codes, quick join and browse public
rooms themselves, through four helpers in `@irtio/client`. Each part is
a project setting, all on by default but quick join:

| Setting | Default | Lets clients |
|---|---|---|
| `players.guests` | `true` | Connect, and use the helpers below, with no player token |
| `players.createRooms` | `true` | Create rooms with `createRoom`, or by connecting to a room ID that doesn't exist, and find them with `joinByCode` |
| `players.listPublic` | `true` | List active public rooms with `listPublic` |
| `players.quickJoin` | `false` | Join a public room with space, or a new one, with `quickJoin` |

Listing needs a player token when guests are off. A game whose rooms
require a login but whose room directory is public, such as a lobby
page, turns on `players.listPublicWithoutToken`: then `listPublic` works
with no token, while connecting still needs one.
`players.readLeaderboardsWithoutToken` does the same for reading
leaderboards with `clientRead: true`; see
[Player data](/docs/room/player-data).

Set them in [`irt.config.ts`](/docs/tools/cli#settings). Turn one off and the
matching calls fail with 403, or 4001 for a guest's connection, so a
project can move to a backend one part at a time.

A player's progress, what players make and high score tables need no
backend either: [player data, collections and
leaderboards](/docs/room/player-data) are kept per project, and a
guest is the same player in rooms and in data calls.

## Guests

A client that connects without a `token` is a guest. The router gives it
a player ID starting with `guest:` and a guest token, which the client
SDK keeps and sends on every connect:

- It's kept in `localStorage` under `irt.guest.<project>` (in
  `sessionStorage` with `guest: "tab"`; see below), so a guest is
  the same player across reconnects, reloads, tabs and browser
  restarts, and keeps its private state, the entities it owns and its
  voice identity.
- It lasts 30 days, and each connect renews it with the same player ID,
  so only a guest away for 30 days comes back as a new one. A token that
  has expired, or that the router can't verify, starts a new guest
  rather than failing.
- Connects and data calls that start together before any token is kept,
  as React's Strict Mode does by mounting twice, wait for the first to
  get its token, so they're one guest too.
- Where `localStorage` is missing or blocked, as in Node or some private
  windows, the token is kept in memory: the guest lasts as long as the
  client object, across its reconnects.

Because every tab of a browser is the same guest, two tabs in one room
are one player: the newer connection closes the older with 4012. Tell
players who see 4012 that they're playing elsewhere. To test two
players in one browser, connect with `guest: "tab"`, which keeps the
token in `sessionStorage` instead, so each tab is its own guest, or use
a private window or another browser.

Guests are anonymous. Anyone can become a new guest at any time by
clearing storage, so don't give a guest anything worth farming, and
treat what a guest sends as untrusted, as you would any client's.

## Creating and joining rooms

With `players.createRooms` on, connecting to a room ID that doesn't
exist creates the room, deployed as under `irt dev`: a private room with
no join code at the project's `rooms.size`. The connection waits while
the room starts, as for a wake. It counts against the address's 10
creates a minute, like `createRoom`; past that the connection closes
with 4005 and the client retries after backoff. With the setting off,
connecting to a room that was never created closes with 4002, and rooms
are created by you with `irt rooms create <id>` or by a backend through
the [HTTP API](/docs/http#rooms).

Use `createRoom` for a room with a join code, a public room, `meta` or
`maxPlayers`:

```ts
// Each takes { project?, token?, signal? } as well: the project URL, by
// default the page's irt-project meta tag; a player token; and an AbortSignal.
function createRoom(init?: {
  id?: string;           // default: a random 12-character ID
  type?: string;         // the room type; default the build's type named default
  public?: boolean;      // listed by listPublic while it's active; default false
  joinCode?: boolean;    // give it a 6-character join code; default false
  meta?: unknown;        // JSON up to 1 KB, returned by listPublic
  maxPlayers?: number;   // cap on connections, 1 to the room size's limit; default the limit
}): Promise<{ id: string; url: string; joinCode: string | null }>;

function joinByCode(code: string, init?: {}): Promise<string>;   // the room's URL
function listPublic(init?: { type?: string }): Promise<PublicRoom[]>;
// maxPlayers is the room's own limit, else its size's connection limit
interface PublicRoom { id: string; url: string; type: string; players: number; maxPlayers: number; meta: unknown }

function quickJoin(init?: {
  type?: string;                    // the room type; default the build's type named default
  meta?: Record<string, unknown>;   // only rooms whose meta has these keys and values; a new room's meta
  maxPlayers?: number;              // a new room's maxPlayers
}): Promise<{ id: string; url: string; joinCode: string | null }>;
```

Each returns room URLs, `https://rooms.irt.io/<project>/<room ID>`,
which `connect` takes as they are. A page with the `irt-project` meta
tag, as every page of an IRT site and under `irt dev` has, passes no
project; elsewhere, write the tag or pass `project`. See [Hosting your
page](/docs/hosting/pages).

A room menu with all four:

```ts
// rooms-menu.ts
import { connect, createRoom, joinByCode, listPublic, quickJoin, type RoomClient } from "@irtio/client";
import { schema } from "./schema.js";

/** Creates a private room and returns its join code for the host to share. */
export async function host(): Promise<{ room: RoomClient; code: string }> {
  const { url, joinCode } = await createRoom({ joinCode: true });
  return { room: await connect(url, { schema }), code: joinCode! };
}

/** Joins the room a friend's code names. */
export async function join(code: string): Promise<RoomClient> {
  return connect(await joinByCode(code), { schema });
}

/** Opens a public room others can find, described by its meta. */
export async function open(mode: string): Promise<RoomClient> {
  const { url } = await createRoom({ public: true, meta: { mode } });
  return connect(url, { schema });
}

/** Joins whichever public room of this mode has space, or opens one. */
export async function play(mode: string): Promise<RoomClient> {
  const { url } = await quickJoin({ meta: { mode } });
  return connect(url, { schema });
}

/** The public rooms with space, fullest first. */
export async function browse(max: number) {
  const rooms = await listPublic();
  return rooms.filter((r) => r.players < max).sort((a, b) => b.players - a.players);
}
```

```ts
// schema.ts
import { defineSchema, entity } from "@irtio/client";

export const schema = defineSchema({ state: entity("scoreboard", { round: "u16" }) });
```

- **Room IDs.** Leave out `id` and the router picks a random one, which
  is hard to guess. Anyone who knows a room's ID can connect to it while
  guests are allowed, so a private room's ID should stay private; share
  its join code, or a link to the game's page with it, instead. A room's
  URL isn't a share link: opened in a browser, it's an error page.
- **Join codes** are 6 characters of `A-H`, `J-N`, `P-Z` and `2-9`, with
  no `I`, `O`, `0` or `1`. `joinByCode` takes them in any case and with
  spaces around them. A code lasts as long as its room; after the room is
  deleted the code gives 404. Room code reads it as `room.joinCode`, for
  example to put it in `room.state` so players who join later, and a
  host who reloads, can show it in the lobby.
- **Public rooms** are listed only while active, with their player count
  and `meta`. A hibernated room has no players and isn't listed, so a
  public room that empties drops off the list until someone connects to
  it again. Room code takes its room off the list, and off quick join,
  with `room.setPublic(false)`; see the
  [room SDK reference](/docs/room/sdk#room). `meta` is set at creation and doesn't change; keep what
  changes, such as a round or a map, in `room.state`.
- **Quick join** is the usual way into an .io game: one button, and
  the player is in a room with others. `quickJoin` picks the active
  public room with the most players that has space, among those whose
  `meta` has every key of `opts.meta` with the same value; without one
  a hibernated such room; and without that it creates a public room
  with `opts.meta` and `opts.maxPlayers`. It holds the room's slot for
  10 seconds or until the client connects, so players who quick join at
  once don't overfill a room or each create one. It's off by default,
  since a game whose rooms aren't open to anyone mustn't let strangers
  in: turn on `players.quickJoin` to use it. A hibernated room has no
  players, so a turn-based game whose players closed the page looks
  empty: its room code calls `room.setPublic(false)` once the game
  starts, which keeps quick join and the public list away from it,
  active or hibernated, until it calls `room.setPublic(true)`. See the
  [client SDK reference](/docs/client/sdk#room-wide-requests-for-games-without-a-backend).
- **Who's in a room** is the room's business: the room decides in
  `onJoin` whether to keep a connection, and can `room.kick` it, which
  closes it with 4006. Room size caps the connections, and `maxPlayers`
  caps them lower, such as 2 for a duel; past either clients get 4003.
  A player who reconnects takes their own place and doesn't count twice.

## A ready-made room screen

For a room screen without writing one, the [UI kit](/docs/client/ui-kit) in
`@irtio/client/ui` has custom elements that use the helpers above:
`<irt-room-panel>` with create, join by code and quick join buttons, the
room's join code with a share link and QR code, the ready state,
the players and a name field; `<irt-room-browser>` for the public rooms;
`<irt-invite>`, `<irt-lobby>`, voice controls, a network indicator and a
join loader. They show the router's message when a setting is off.

## Rooms that come and go

Client-created rooms pile up unless something deletes them.
`rooms.retention` deletes a room after it has been hibernated that long:

| `rooms.retention` | Suits |
|---|---|
| `"none"` | Rooms that matter only while players are in them, such as a single round; deleted 30 seconds after the last player leaves |
| `"7d"` (default), `"<n>h"`, `"<n>d"` | Rooms players come back to, such as a party's room or a saved game |
| `"forever"` | Rooms that are places, such as a persistent world |

A project can hold at most `limits.maxRooms` rooms, 1,000 by default, so
retention matters most for games where every round creates a room. Over
it, `createRoom` fails with 429 until rooms expire. See
[Limits](/docs/hosting/limits#project-settings-with-limits).

## Errors

Each helper rejects with an `IrtError` whose `code` is the HTTP status
and whose `name` is the web's name for it: 403, a `NotAllowedError`,
when the setting is off, as `quickJoin` does until you turn it on, 404 for an unknown join code, 409 for an
`id` that's taken, 429 when rate limited or at `maxRooms`. The full list
is in [Errors and close codes](/docs/client/errors#room-wide-requests).

## Testing

`@irtio/testing`'s host has the same four calls, with the project's
settings applied, so a test can walk through a room menu without a network:

```ts
// rooms-menu.test.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";
import { createTestHost } from "@irtio/testing";
import { expect, test } from "vitest";

const room = defineRoom({ schema: defineSchema({ state: entity("scoreboard", { round: "u16" }) }) });

test("a friend joins by code, and a public room is listed while it's active", async () => {
  const host = createTestHost(room);
  const { id, joinCode } = await host.createRoom({ joinCode: true });
  const alice = await host.room(id).connect({ player: "alice" });
  const bob = await host.room(await host.joinByCode(joinCode!.toLowerCase())).connect({ player: "bob" });
  expect(bob.client.connection).not.toBe(alice.client.connection);

  const open = await host.createRoom({ public: true, meta: { mode: "duel" } });
  expect(await host.listPublic()).toEqual([]);
  await host.room(open.id).connect({ player: "carol" });
  expect(await host.listPublic()).toEqual([{ id: open.id, players: 1, maxPlayers: 64, type: "default", meta: { mode: "duel" } }]);
});

test("quick join fills one public room before opening another", async () => {
  const host = createTestHost(room, { settings: { players: { quickJoin: true } } });
  const first = await host.quickJoin({ meta: { mode: "duel" }, maxPlayers: 2 });
  await host.room(first.id).connect({ player: "alice" });
  expect((await host.quickJoin({ meta: { mode: "duel" } })).id).toBe(first.id);
  await host.room(first.id).connect({ player: "bob" });
  expect((await host.quickJoin({ meta: { mode: "duel" } })).id).not.toBe(first.id);
});

test("clients can't create rooms when the setting is off", async () => {
  const host = createTestHost(room, { settings: { players: { createRooms: false } } });
  await expect(host.createRoom()).rejects.toMatchObject({ code: 403 });
});
```

See [Testing rooms](/docs/guides/testing) for more.

## Adding a backend later

Accounts come from your own backend, which signs
[player tokens](/docs/http/player-tokens). Pass a token to `connect` and to the
helpers, and that client is a known player rather than a guest. Turning
`players.guests` off then requires one; turning `players.createRooms`
off leaves room creation to the backend, through the HTTP API.

## Common mistakes

- **Turning `players.createRooms` off and connecting to a fixed room ID.**
  Nothing creates the room any more, so connecting gives 4002. Create it
  with `irt rooms create` or from your backend.
- **Testing two players in two tabs.** They're the same guest; the newer
  tab closes the older with 4012. Connect with `guest: "tab"`, or use a
  private window.
- **Expecting a public room to stay listed while empty.** It hibernates
  30 seconds after its last player leaves and drops off the list.
- **Trusting a guest.** A guest ID costs nothing to replace. Validate
  messages in the room and keep anything valuable behind player tokens.
- **Leaving retention at 7 days for throwaway rooms.** Every such
  room counts toward `maxRooms` until it expires. Use `"none"`.
