dataClient() function
@irtio/client 5.0BrowsersNodeThe dataClient() function returns a DataClient: the player’s data and the project’s collections and leaderboards, for the player the client is, with no room connection needed.
Syntax
dataClient()
dataClient(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 from your backend. Without one, the client is a guest, the same one as its room connections.
guest- A string: where a guest’s token is kept.
"tab"keeps it insessionStorage, so each tab is its own player, and"browser"keeps it inlocalStorage, so every tab of the browser is one player. Unused with atoken. Defaults to"tab". signal- An
AbortSignalthat aborts every request of the client. Each call also takes asignalof its own, for that call. A request either aborts rejects with aDataErrorwith codeE_UNAVAILABLE, namedAbortError, orTimeoutErrorfor anAbortSignal.timeout(), with the signal’s reason as itscause.
Return value
A DataClient.
Exceptions
TypeError- Thrown if there’s no project URL, no
projectgiven and noirt-projectmeta tag, or if the project URL’s path doesn’t end in a project.
Description
The calls go over the router’s POST /v1/data/{project}, and are the same as the Room API’s PlayerData, Collection and Leaderboard calls, limited to what a client may do. Nothing is cached on the client: every call goes to the router. A connected RoomClient has the same calls, as room.playerData, room.collection() and room.leaderboard(), made with the connection’s token or guest.
Without a token, the first call takes the guest token connect() keeps for the same guest setting, or gets one from POST /v1/guest and keeps it there, so a guest is one player in rooms and data calls. An expired guest token gets a new guest, as connecting does. With storage blocked, the token lives as long as the page.
A leaderboard’s top(), and around() with a player, go without a token until the client has one. With the project’s players.readLeaderboardsWithoutToken on, they need no guest; otherwise the router’s 401 gets one first.
Calls reject with a DataError: E_UNAVAILABLE when the router or the data service can’t be reached or answers 503; E_LIMIT over the router’s 10 calls a second per address and per player, or 30 guests a minute per address for each project; and E_FORBIDDEN for a rejected token or a call the project’s settings don’t allow a client. See Errors and close codes.
Examples
Showing the week’s best on the start screen
A start screen shows a leaderboard before the player joins any room.
// start-screen.ts
import { dataClient, DataError } from "@irtio/client";
const data = dataClient();
export async function weekBoard() {
try {
const { entries } = await data.leaderboard("laps").top({ period: "week", limit: 5 });
return entries.map((e) => `${e.rank}. ${e.player} ${e.score} ms`);
} catch (e) {
if (e instanceof DataError && e.code === "E_UNAVAILABLE") return [];
throw e;
}
}
See also
DataClientClientLeaderboard- Player data, collections and leaderboards, for what a client may read and write
- Games without a backend, for how guests work
Runtime availability
| Browsers | Node | |
|---|---|---|
dataClient() | 5.0 | 5.0 |