# PresenceList

> The PresenceList interface of the Client API lists who is in the room, an entry per player with a name and a status, and follows them joining, changing and leaving.

The **`PresenceList`** interface of the [Client API](/docs/client) lists who is in the room: a [`PresenceEntry`](/docs/client/presence-entry) 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`](/docs/client/room-client/presence).

Game code never constructs a `PresenceList`. The client makes one when it's constructed, and fills it once [`ready`](/docs/client/room-client/ready) resolves.

## Instance properties

[`PresenceList.me`](/docs/client/presence-list/me)
: The local player's entry, or `undefined` before the client is first ready. Read only.

[`PresenceList.size`](/docs/client/presence-list/size)
: The number of entries. Read only.

[`PresenceList.leader`](/docs/client/presence-list/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](/docs/client/presence-list/leader-event).

## Instance methods

[`PresenceList.get()`](/docs/client/presence-list/get)
: Returns a player's entry, or `undefined`.

[`PresenceList.setStatus()`](/docs/client/presence-list/setstatus)
: Sets the local player's status, which every connection sees.

## Events

Listen to these events with `addEventListener()`.

[`join` event](/docs/client/presence-list/join-event)
: A player joined the room.

[`change` event](/docs/client/presence-list/change-event)
: A player's entry changed; `changed` names the fields.

[`leave` event](/docs/client/presence-list/leave-event)
: A player left the room.

[`leader` event](/docs/client/presence-list/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](/docs/room/guides/state-sync#interest) says, unless the room defines [`seesPresence()`](/docs/room/room-definition/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()`](/docs/client/room-client/close).

The list changes a whole state update at a time. Its events fire after the [`EntityStore`](/docs/client/entity-store) callbacks of the same state update, and before the client's [`update` event](/docs/client/room-client/update-event). After a resync, players who stayed don't leave and join again: the [`change` event](/docs/client/presence-list/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.

```ts
// 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

- [`PresenceEntry`](/docs/client/presence-entry)
- [`Presence`](/docs/room/presence), the room's side
- [Presence](/docs/room/guides/presence)
