PresenceList: change event

Available since @irtio/client 5.0BrowsersNode

The change event of the PresenceList interface fires each time a player’s presence entry changes, with the names of the fields that changed.

Syntax

addEventListener("change", (event) => {})

Event type

A PresenceEvent. It extends Event and adds these properties:

entry Read only
The PresenceEntry that changed.
changed Read only
An array of the fields that changed: any of "name", "status" and "connected".

Description

An entry changes when the room sets a player’s name or status with Presence.set(), when a player sets their status with setStatus(), and when connected turns false as a player’s last connection drops, or true again as they reconnect within the 10-second grace.

The local player’s own setStatus() doesn’t fire it, since the client’s copy changes at once. A correction from the room, replacing the local player’s status, does.

After a resync, change fires for players who stayed, with whatever changed while the client was away, rather than leave and join.

Examples

Dimming players who lose connection

The client greys out a player while their connection is down, and shows their status as it changes.

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

const room = await connect("main");

room.presence.addEventListener("change", ({ entry, changed }) => {
  if (changed.includes("connected")) console.log(`${entry.player} ${entry.connected ? "is back" : "lost connection"}`);
  if (changed.includes("status")) console.log(`${entry.player} is now`, entry.status);
});

See also

Runtime availability

BrowsersNode
change event5.05.0