PlayerData: refresh() method

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The refresh() method of the PlayerData interface drops what the room’s cache holds of the player’s data, so the next get() of each key reads it from the data service.

Syntax

refresh()

Parameters

None.

Return value

A Promise that resolves with undefined once the cache is dropped.

Description

Without refresh(), a write made outside the room, by a client, another room or your backend, shows in the room’s get() within 5 seconds. Call it when the room knows such a write happened and needs it at once.

Examples

Reading a purchase the shop just made

The client buys through your backend, then tells the room.

// purchases.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  async onMessage(room, connection, message) {
    if (message !== "bought") return;
    const data = room.playerData(connection.player);
    await data.refresh();
    const gems = await data.get<number>("gems");
    room.send(connection, { type: "gems", gems: gems?.value ?? 0 });
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
refresh()0.10.10.10.1