PresenceList: leader event
@irtio/client 5.0BrowsersNodeThe leader event of the PresenceList interface fires when the player who leads a native room changes. Read the new leader from the list’s leader property.
Syntax
addEventListener("leader", (event) => {})
Event type
A generic Event.
Description
leader is the player ID of the player who leads a native room, or undefined when none does or the client isn’t connected. A native room names its leader after the client’s first state update, and the event fires then and whenever the leader changes.
The leader keeps the role while their presence lasts, so through a disconnect of up to 10 seconds, or until they pass it with room.lead(player); then the earliest connected player leads, and the role doesn’t go back to the old leader. A room with room code has no leader, so leader stays undefined and the event doesn’t fire.
Examples
Marking the leader in a player list
The list redraws when someone joins, changes or leaves, and when the leader changes.
// leads.ts
import { connect } from "@irtio/client";
const room = await connect("main");
function draw(): void {
const names = [...room.presence].map((entry) => (entry.player === room.presence.leader ? `${entry.name} (leads)` : entry.name));
console.log(names.join(", "));
}
draw();
for (const type of ["join", "change", "leave", "leader"] as const) room.presence.addEventListener(type, draw);
See also
RoomClient.startCountdown(), which only the leader of a lobby can call- Presence
Runtime availability
| Browsers | Node | |
|---|---|---|
leader event | 5.0 | 5.0 |