createRoom() function
@irtio/client 5.0BrowsersNodeThe createRoom() function creates a room from the client, for games without a backend, and resolves with the room’s ID, URL and join code. Pass the URL to connect().
Syntax
createRoom()
createRoom(init)
Parameters
initOptional-
An object with any of these properties:
project- A string or a
URL: the project URL, such as"https://rooms.irt.io/my-game/". Defaults to the page’sirt-projectmeta tag. token- A string: a player token. Guests leave it out.
signal- An
AbortSignalthat aborts the request. id- A string: the room’s ID. Defaults to a random 12-character ID, which is hard to guess.
type- A string: the room type to create. The type’s own
maxPlayersand size apply. Defaults to the type nameddefault. public- A boolean. If
true, the room is on quick join and the public room list while it’s active. Defaults tofalse. joinCode- A boolean. If
true, the room gets a 6-character join code. Defaults tofalse. meta- Any JSON value, at most 1 KB: the room’s meta, which
listPublic()returns,quickJoin()matches on and room code reads asroom.meta. It’s fixed for the room’s life. maxPlayers- A number from 1 to the room size’s connection limit (64 for
standard) that caps the room’s connections. A connection past it closes with 4003. Defaults to the limit.
Return value
A Promise that resolves with an object with these properties:
id- The room’s ID.
url- The room’s URL, absolute, to pass to
connect(). joinCode- The room’s join code, or
nullif it has none.
Exceptions
TypeError- Rejects with this if there’s no project URL, no
projectgiven and noirt-projectmeta tag; if the project URL’s path doesn’t end in a project; or if it isn’t anhttp:,https:,ws:orwss:URL. IrtError- Rejects with this if the router refuses the request, with the HTTP status as its
code: 400 for an invalid room ID,metaover 1 KB, amaxPlayersout of range or notypewhen the build has no type nameddefault; 401 for an invalid token; 403, aNotAllowedError, when the project’splayers.createRoomsis off; 409 for anida room already has; 429, aQuotaExceededError, when the address is rate limited or the project haslimits.maxRoomsrooms; and 503 when no join code was free. If the request didn’t complete, itscodeis 0 and itsnameisTimeoutErrorwhensignalcame fromAbortSignal.timeout()and aborted,AbortErrorwhen anothersignalaborted, with its reason ascause, andNetworkErrorotherwise, with the failedfetch()’s error ascause.
Description
createRoom() needs the project’s players.createRooms setting on. With it on, connecting to a room that doesn’t exist also creates it, as a private room with no join code, so createRoom() is for a room with a join code, a public room, meta or maxPlayers.
Anyone who knows a room’s ID can connect to it, so share a private room’s join code instead, or a link to the game’s page with it. Join codes are 6 characters of A-H, J-N, P-Z and 2-9, and last as long as their room.
Examples
Hosting a duel with a join code
The host creates a room for two players with a join code to tell a friend, and backs off when the project is creating rooms too fast.
// host-duel.ts
import { connect, createRoom, IrtError } from "@irtio/client";
export async function hostDuel() {
try {
const { url, joinCode } = await createRoom({ joinCode: true, maxPlayers: 2 });
const room = await connect(url);
console.log(`tell your friend the code ${joinCode}`);
return room;
} catch (e) {
if (e instanceof IrtError && e.code === 429) {
console.warn("too many new rooms; try again in a minute");
return undefined;
}
throw e;
}
}
Opening a public match of another type
The room is created of the match type and listed publicly with its map, so the client connects with the same type.
// open-match.ts
import { connect, createRoom } from "@irtio/client";
export async function openMatch(map: string) {
const { url } = await createRoom({ type: "match", public: true, meta: { map } });
return connect(url, { type: "match" });
}
See also
joinByCode()quickJoin()Room.setPublic(), for the room to take itself off the list- Games without a backend
- Errors and close codes
Runtime availability
| Browsers | Node | |
|---|---|---|
createRoom() | 5.0 | 5.0 |