RoomClient: removeEventListener() method

Available since @irtio/client 5.0BrowsersNode

The removeEventListener() method of the RoomClient interface removes a listener that addEventListener() added, as it does on any EventTarget.

Syntax

removeEventListener(type, listener)
removeEventListener(type, listener, options)

Parameters

type
A string, the name of the event the listener was added for, such as "message".
listener
The function or object that was added. A listener that isn’t there, or null, removes nothing.
options Optional
A boolean, which sets capture, or an object with a capture property. It must match the capture the listener was added with, which is false unless set.

Return value

None (undefined).

Exceptions

None.

Description

A listener added with a signal is also removed when the signal aborts, and one added with once after it runs once. To remove several listeners together, add them with one AbortController’s signal rather than keeping each function to remove.

Removing the last message listener doesn’t make the client hold messages again: it holds them only until the first listener is added.

Examples

Ending a quiz round

A quiz listens for questions until the room says the quiz is over, then removes its listener so the next mini-game gets the messages alone.

// quiz.ts
import type { ReadyRoomClient, RoomMessageEvent } from "@irtio/client";

export function playQuiz(room: ReadyRoomClient) {
  function onQuizMessage(event: RoomMessageEvent) {
    const m = event.data as { type?: string; question?: string };
    if (m.type === "question") console.log(m.question);
    if (m.type === "quiz-over") room.removeEventListener("message", onQuizMessage);
  }
  room.addEventListener("message", onQuizMessage);
}

See also

Runtime availability

BrowsersNode
removeEventListener()5.05.0