Presence: set() method

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

The set() method of the Presence interface sets a player’s presence name or status. Every client that sees the entry gets the change in the next state update.

Syntax

set(player, values)

Parameters

player
The player ID.
values

An object with any of these properties. A property left out keeps its value.

name
A string of at most MAX_PRESENCE_NAME (64) characters.
status
Any value JSON can encode, at most MAX_PRESENCE_STATUS (1,024) bytes as JSON. undefined sets it to null.

Return value

None (undefined).

Exceptions

Error
Thrown if the player has no presence entry.
TypeError
Thrown if name isn’t a string of at most 64 characters, or status can’t be encoded as JSON.
RangeError
Thrown if status is over 1,024 bytes as JSON.

Description

The name is the room’s to set. It starts as the name string of the token’s meta, when there’s one of at most 64 characters, and "" otherwise.

The status is the player’s own, which it sets from its client, and no write handler runs for it. The room checks it after the fact and replaces it with set(). A status the room sets that differs from what the player wrote reaches the player as a correction.

Setting a value the entry holds already sends nothing.

Examples

Letting a player pick a nickname

// nickname.ts
import { defineRoom, MAX_PRESENCE_NAME } from "@irtio/room";

const BLOCKED = ["admin", "moderator"];

export default defineRoom({
  onMessage(room, connection, message) {
    const nick = (message as { nick?: unknown } | null)?.nick;
    if (typeof nick !== "string") return;
    const name = nick.trim().slice(0, MAX_PRESENCE_NAME);
    if (name === "" || BLOCKED.includes(name.toLowerCase())) return room.send(connection, { type: "nick-refused" });
    room.presence.set(connection.player, { name });
  },
});

See also

Runtime availability

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