DataClient

Available since @irtio/client 5.0BrowsersNode

The DataClient interface of the Client API is the player’s data and the project’s collections and leaderboards, for the player the client is, over the router’s HTTP API with no room connection. dataClient() returns one.

A connected RoomClient has the same calls, made with the connection’s token or guest.

Instance properties

DataClient.playerData
The player’s data. Read only.

Instance methods

DataClient.collection()
Returns one of the project’s collections.
DataClient.leaderboard()
Returns one of the project’s leaderboards.
DataClient.player()
Resolves with the player ID the calls are made as.

Description

A client may do less than room code:

  • It reads its own player data, and writes only the keys in the project’s data.player.clientWrite.
  • It gets and lists a collection’s entries and reads a board with the collection’s clientRead: true, or only the entries it owns with clientRead: "own".
  • With clientWrite: "own", it creates entries, changes and deletes its own, and submits its own score. owner is always the client’s player, and a create past the collection’s maxOwn rejects with E_QUOTA.
  • Its writes can’t use once, which rejects with E_INVALID.
  • It makes at most 10 calls a second per address and per player, and an address gets at most 30 guests a minute for each project; past that, calls reject with E_LIMIT.

Nothing is cached on the client: every call goes to the router, and refresh() has nothing to drop. Calls reject with a DataError, as dataClient() describes.

Examples

Saving settings before joining a room

The settings screen saves the player’s volume as player data, and reports when the router can’t be reached.

// settings.ts
import { dataClient, DataError } from "@irtio/client";

const data = dataClient();

export async function saveVolume(volume: number) {
  try {
    await data.playerData.set("settings", { volume });
    return true;
  } catch (e) {
    if (e instanceof DataError && e.code === "E_UNAVAILABLE") return false;
    throw e;
  }
}

set() needs settings in the project’s data.player.clientWrite; any other key rejects with E_FORBIDDEN.

See also

Runtime availability

BrowsersNode
DataClient5.05.0