RoomClient: leaderboard() method
@irtio/client 5.0BrowsersNodeThe leaderboard() method of the RoomClient interface returns a ClientLeaderboard: a collection declared with leaderboard settings in irt.config.ts, which keeps players’ scores per period, read and submitted to as this player.
Syntax
leaderboard(name)
Parameters
name- A string: the collection’s name, as
irt.config.tsdeclares it.
Return value
A ClientLeaderboard, whose submit(), top() and around() methods return promises. Getting it makes no request, and doesn’t check name: a collection that isn’t declared, or has no leaderboard settings, fails each call with E_UNKNOWN.
Exceptions
None.
Description
The board’s calls go to the router over HTTP, with the connection’s token or guest, so they need no open connection to the room. They’re the same calls dataClient() makes. Nothing is cached on the client: every call goes to the router.
A client submits only its own score, and only to a board with clientWrite: "own". A score a client submits can be faked, so for a score that matters, submit it from room code with room.leaderboard() instead.
Calls reject with DataError, which @irtio/client also exports: E_UNAVAILABLE when the router or the data service can’t be reached, E_LIMIT over the router’s 10 calls a second per address and per player and E_FORBIDDEN for what the board’s settings don’t allow.
Examples
Showing the player’s rank after a run
A casual puzzle game, whose board takes scores from clients, submits the player’s score and shows their rank with the scores either side.
// rank.ts
import { DataError, type ReadyRoomClient } from "@irtio/client";
export async function showRank(room: ReadyRoomClient, score: number) {
const board = room.leaderboard("daily-puzzle");
try {
await board.submit(score);
const around = await board.around({ period: "day", count: 2 });
if (around) console.log(`You're number ${around.rank} today`);
} catch (e) {
if (e instanceof DataError && e.code === "E_UNAVAILABLE") console.warn("The leaderboard can't be reached.");
else throw e;
}
}
See also
ClientLeaderboardRoomClient.collection()Room.leaderboard(), for scores that can’t be faked- Player data, collections and leaderboards
Runtime availability
| Browsers | Node | |
|---|---|---|
leaderboard() | 5.0 | 5.0 |