PresenceList

Available since @irtio/client 5.0BrowsersNode

The PresenceList interface of the Client API lists who is in the room: a PresenceEntry for each player, with a name and a small status. It sets the local player’s status and fires events as players join, change and leave. Each client has one, as room.presence.

Game code never constructs a PresenceList. The client makes one when it’s constructed, and fills it once ready resolves.

Instance properties

PresenceList.me
The local player’s entry, or undefined before the client is first ready. Read only.
PresenceList.size
The number of entries. Read only.
PresenceList.leader
The player who leads a native room, or undefined when none does or the client isn’t connected. Read only. See the leader event.

Instance methods

PresenceList.get()
Returns a player’s entry, or undefined.
PresenceList.setStatus()
Sets the local player’s status, which every connection sees.

Events

Listen to these events with addEventListener().

join event
A player joined the room.
change event
A player’s entry changed; changed names the fields.
leave event
A player left the room.
leader event
A native room’s leader changed.

Description

PresenceList is iterable: for (const entry of room.presence) visits every entry, connected or not, bots included. Spread it, [...room.presence], for an array.

It lists every player in the room, whatever the room’s interest says, unless the room defines seesPresence(). The room runtime adds a player’s entry when their first connection opens and removes it 10 seconds after their last connection drops, or at once when their client closes with close().

The list changes a whole state update at a time. Its events fire after the EntityStore callbacks of the same state update, and before the client’s update event. After a resync, players who stayed don’t leave and join again: the change event fires for whatever changed while the client was away.

Examples

Drawing a player list

The client draws the list once it’s connected, then redraws it whenever someone joins, changes or leaves.

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

const room = await connect("main");

function draw(): void {
  const names = [...room.presence].map((entry) => `${entry.name || entry.player}${entry.connected ? "" : " (away)"}`);
  console.log(`${room.presence.size} here: ${names.join(", ")}`);
}

draw();
for (const type of ["join", "change", "leave"] as const) room.presence.addEventListener(type, draw);

See also

Runtime availability

BrowsersNode
PresenceList5.05.0