quickJoin() function
@irtio/client 5.0BrowsersNodeThe quickJoin() function finds a public room with space, or creates one, and resolves with its ID, URL and join code, for games that fill rooms with whoever comes. Pass the URL to connect().
Syntax
quickJoin()
quickJoin(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. type- A string: the room type to pick or create. Defaults to the type named
default. meta- An object. Only rooms whose
metahas every one of its top-level keys, with an equal value, are picked, and a new room gets it as itsmeta. maxPlayers- A number: a new room’s
maxPlayers, from 1 to the room size’s connection 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: 403, aNotAllowedError, when the project’splayers.quickJoinis off; 400 formetaover 1 KB, amaxPlayersout of range or notypewhen the build has no type nameddefault; 401 for an invalid token; and 429, aQuotaExceededError, when the address is rate limited or the project haslimits.maxRoomsrooms. 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
quickJoin() picks the active public room with the most players that’s under its connection limit and its own maxPlayers, among those whose meta matches. Without one, it picks such a hibernated room, and without that, it creates a public room with meta as its meta and maxPlayers as its 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. A room whose code called room.setPublic(false), such as a game whose seats are taken, isn’t picked, active or hibernated, until it sets it back.
The project’s players.quickJoin setting is off by default, since a game whose rooms aren’t open to anyone mustn’t let strangers in. Matchmaking by skill, parties and queues belong in your backend.
Examples
Playing a mode with one button
The play button puts the player in a room of the mode they picked, and the same signal cancels both steps.
// play-button.ts
import { connect, IrtError, quickJoin } from "@irtio/client";
export async function play(mode: "duel" | "squad", signal: AbortSignal) {
try {
const { url } = await quickJoin({ meta: { mode }, maxPlayers: mode === "duel" ? 2 : 8, signal });
return await connect(url, { signal });
} catch (e) {
if (e instanceof IrtError && e.name === "AbortError") return undefined;
throw e;
}
}
maxPlayers applies only if quickJoin() creates the room. A room it finds keeps its own.
See also
Runtime availability
| Browsers | Node | |
|---|---|---|
quickJoin() | 5.0 | 5.0 |