listPublic() function
Available since
@irtio/client 5.0BrowsersNodeThe listPublic() function resolves with the project’s active public rooms, each with its player count and meta, for a room browser.
Syntax
listPublic()
listPublic(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 list. Without one, every type is listed.
Return value
A Promise that resolves with an array of objects, one for each room, with these properties:
id- The room’s ID.
url- The room’s URL, absolute, to pass to
connect(). type- The room’s type.
players- The number of players in the room.
maxPlayers- The room’s own
maxPlayers, or its room size’s connection limit if it has none. meta- The meta the room was created with.
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.listPublicis 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 is public when createRoom() created it with public: true, or when quickJoin() created it. Only active rooms are listed: a hibernated room has no players, so a public room that empties drops off the list until someone connects to it again. A room whose code called room.setPublic(false), such as a game whose seats are taken, isn’t listed until it sets it back.
Examples
Listing duels with a free seat
A room browser shows the duels a player can still join, fullest first.
// duel-browser.ts
import { listPublic } from "@irtio/client";
export async function openDuels() {
const rooms = await listPublic({ type: "duel" });
const open = rooms.filter((r) => r.players < r.maxPlayers);
open.sort((a, b) => b.players - a.players);
for (const r of open) console.log(`${r.id}: ${r.players} of ${r.maxPlayers}`);
return open;
}
See also
quickJoin(), to join the best of them in one stepcreateRoom()- Games without a backend
- UI kit, for a ready-made room browser
Runtime availability
| Browsers | Node | |
|---|---|---|
listPublic() | 5.0 | 5.0 |