RoomClient: voice event

Available since @irtio/client 5.0BrowsersNode

The voice event of the RoomClient interface fires when the router grants voice for this connection, and sets voice.

Syntax

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

Event type

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

url
A string. The voice server’s URL.
token
A string. This connection’s token for the voice server.

Description

The router grants voice only when the project uses voice. It grants it again on each connection, so voice fires again after a reconnect; until then, room.voice is undefined.

joinVoice() from @irtio/client/voice uses room.voice to join the voice server, so most games don’t need this event. Listen for it to know when voice is on offer, such as to enable a microphone button.

Examples

Enabling a microphone button

The button stays disabled until the router grants voice. The grant can arrive before connect() resolves, so the code also checks room.voice.

// mic-button.ts
import { connect } from "@irtio/client";

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

mic.disabled = room.voice === undefined;
room.addEventListener("voice", () => {
  mic.disabled = false;
});
room.addEventListener("reconnecting", () => {
  mic.disabled = true;
});

See also

Runtime availability

BrowsersNode
voice event5.05.0