PresenceList: setStatus() method

Available since @irtio/client 5.0BrowsersNode

The setStatus() method of the PresenceList interface sets the local player’s presence status, a small JSON value every connection in the room sees.

Syntax

setStatus(status)

Parameters

status
Any JSON value of at most 1 KB as JSON, such as { typing: true }, or null to clear it.

Return value

None (undefined).

Exceptions

TypeError
Thrown if status can’t be serialized as JSON.
RangeError
Thrown if status is over 1 KB as JSON.
Error
Thrown if the client has no presence entry yet, before it’s first ready.

Description

setStatus() is an owner write, like setting a writable field of an OwnedEntity. It changes me.status at once, without firing the change event, and the client sends it with its other owner writes: before a call or message, and otherwise at most once per tick in a room with ticks, or at once and then at most once per 16 ms in a room without ticks.

The status keeps a copy of the value, so changing your object afterwards changes nothing. Every other client sees the new status in a state update, and their change event fires for status.

The room has the last word. It can replace any player’s status with Presence.set(), and when it changes this player’s, the client gets a correction and change fires for status. A status is a claim from the player, so keep anything that decides the game out of it; the room’s writes handlers don’t run for it.

Examples

Showing who’s typing

The client sets a typing flag while the chat box has text in it, and clears it when the message is sent.

// typing.ts
import { connect } from "@irtio/client";

const room = await connect("main");
let typing = false;

export function onInput(text: string): void {
  if (typing === (text !== "")) return;
  typing = text !== "";
  room.presence.setStatus({ typing });
}

export function onSend(message: string): void {
  room.send({ type: "chat", message });
  typing = false;
  room.presence.setStatus({ typing });
}

Setting the status only when the flag flips keeps the client from queueing a write on every keystroke.

See also

Runtime availability

BrowsersNode
setStatus()5.05.0