RoomClient: peers event

Available since @irtio/client 5.0BrowsersNode

The peers event of the RoomClient interface fires when peers changes: the players this player hears, or is heard by, as the room decides.

Syntax

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

Event type

A CustomEvent whose detail is the new room.peers, an object with these properties:

speakers
A read-only array of the player IDs this player hears.
listeners
A read-only array of the player IDs that hear this player.

Description

The room decides who hears whom, with its voice settings, and the router sends the result whenever it changes. Each reconnect empties room.peers until the room sends it again, and fires peers with both lists empty if they weren’t already.

peers follows what the room decided, not who is on the voice server. The Voice object of joinVoice() has the players on the voice server.

Examples

Showing who is in earshot

A survival game lists the players this one can hear.

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

const room = await connect("https://rooms.irt.io/my-game/camp");
const earshot = document.querySelector("#earshot")!;

room.addEventListener("peers", ({ detail: peers }) => {
  earshot.textContent = peers.speakers.length === 0 ? "No one in earshot" : `Hearing ${peers.speakers.join(", ")}`;
});

See also

Runtime availability

BrowsersNode
peers event5.05.0