class Room
implements WebSocketRoom
Since 0.1.0

A named group of connections that can be addressed as one.

Constructors

Room(
name: string,
publish?: RoomPublisher
)

Creates a room.

Parameters
name: string

The room name

optional
listener: RoomMembershipListener

Notified on every membership change. Supplied by a RoomRegistry; omit for a standalone room.

optional
publish: RoomPublisher

Forwards broadcasts to other replicas. Omit for a room that stays in-process.

Properties

readonly
name: string

The room name.

readonly
rawSize: number

Total membership including connections that have since closed.

readonly
size: number

Number of currently open members.

Methods

Adds a connection to this room.

broadcast(
data: string | Uint8Array,
): void

Sends a frame to every open member, skipping any closed member and any member named by options.except.

broadcastJson<T>(
payload: T,
): void

Serializes a value to JSON once and broadcasts it as a text frame.

Since 0.2.0
broadcastLocal(
data: string | Uint8Array,
): void

Sends a frame to this replica's own members only, without forwarding it to the backplane.

This is the delivery path for a frame that ARRIVED from another replica: re-publishing it would echo it back around the cluster forever. Applications call Room.broadcast; only the plugin's backplane subscriber calls this.

Exclusion is honored two ways: options.except names a live connection object (the local path), and options.exceptId names one by ID (the path a frame arriving from another replica takes, since the excluded connection object does not exist here).

Removes a connection from this room.

Usage

import { Room } from "websocket-plugin/src/index.ts";