PresenceList: setStatus() method
@irtio/client 5.0BrowsersNodeThe 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 }, ornullto clear it.
Return value
None (undefined).
Exceptions
TypeError- Thrown if
statuscan’t be serialized as JSON. RangeError- Thrown if
statusis 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
PresenceEntryPresence.set(), the room’s side- Status
Runtime availability
| Browsers | Node | |
|---|---|---|
setStatus() | 5.0 | 5.0 |