joinByCode() function
Available since
@irtio/client 5.0BrowsersNodeThe joinByCode() function resolves with the URL of the room a join code names, to pass to connect().
Syntax
joinByCode(code)
joinByCode(code, init)
Parameters
code- A string: the join code, 6 characters of
A-H,J-N,P-Zand2-9. It’s case-insensitive and spaces around it are ignored, so"k7qx2m "findsK7QX2M. 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.
Return value
A Promise that resolves with the room’s URL, a string.
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: 404, aNotFoundError, when no room has the code, its room was deleted or the project doesn’t exist; 403, aNotAllowedError, when the project’splayers.createRoomsis off; 401 for an invalid token; and 429, aQuotaExceededError, when the address is rate limited. 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
A room gets a join code when createRoom() asks for one with joinCode: true. The code lasts as long as its room. Room code reads it as room.joinCode, such as to show it in the lobby.
Examples
Joining a friend’s game
The player types the code their friend shared. A code no room has gets a message rather than an error.
// join-friend.ts
import { connect, IrtError, joinByCode } from "@irtio/client";
export async function joinFriend(code: string) {
try {
return await connect(await joinByCode(code));
} catch (e) {
if (e instanceof IrtError && e.name === "NotFoundError") {
console.warn(`no game has the code ${code.trim().toUpperCase()}`);
return undefined;
}
throw e;
}
}
connect() rejects with a NotFoundError too, for close code 4002, if the room is deleted between the two calls.
See also
createRoom(), for a room with a join code- Games without a backend
- UI kit, for a ready-made join screen
Runtime availability
| Browsers | Node | |
|---|---|---|
joinByCode() | 5.0 | 5.0 |