listPublic() function

Available since @irtio/client 5.0BrowsersNode

The 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

init Optional

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’s irt-project meta tag.
token
A string: a player token. Guests leave it out.
signal
An AbortSignal that 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 project given and no irt-project meta tag; if the project URL’s path doesn’t end in a project; or if it isn’t an http:, https:, ws: or wss: URL.
IrtError
Rejects with this if the router refuses the request, with the HTTP status as its code: 403, a NotAllowedError, when the project’s players.listPublic is off; 401 for an invalid token; and 429, a QuotaExceededError, when the address is rate limited. If the request didn’t complete, its code is 0 and its name is TimeoutError when signal came from AbortSignal.timeout() and aborted, AbortError when another signal aborted, with its reason as cause, and NetworkError otherwise, with the failed fetch()’s error as cause.

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

Runtime availability

BrowsersNode
listPublic()5.05.0