RTCForge API Reference v1.1.1
    Preparing search index...

    A single connected client within a Room.

    Wraps the underlying WebSocket: it parses and validates inbound ClientMessages, applies optional per-peer rate limiting, tracks the last heartbeat pong for liveness, and serializes outbound ServerMessages. It extends the core EventEmitter and emits PeerEvent values (signal, broadcast, pong, error, rate-limit-exceeded, disconnected). Peers are created and owned by SignalingServer; you usually obtain them via Room.getPeers.

    Hierarchy (View Summary)

    Index

    Constructors

    • Wraps an open WebSocket as a peer and begins listening for inbound messages and the socket close event.

      Parameters

      • opts: PeerOptions

        Peer configuration; see PeerOptions.

      Returns Peer

    Properties

    id: string

    Stable identifier for this peer.

    metadata: Record<string, string>

    Immutable (frozen) copy of the metadata supplied at join time.

    Accessors

    • get lastPong(): number

      Epoch-millisecond timestamp of the peer's most recent heartbeat pong (initialized to construction time). Used by the heartbeat monitor to prune unresponsive peers.

      Returns number

    • get role(): string

      The peer's current role. Empty string if none was assigned.

      Returns string

    Methods

    • Marks the peer as admitted to its room.

      Returns void

      Called by SignalingServer only after the peer has been successfully added to a Room. Until then, inbound signal and broadcast frames are ignored so an un-admitted (or about-to-be-rejected) peer cannot relay or broadcast during the async admission window.

    • Parameters

      • code: number
      • reason: string

      Returns void

    • Synchronously invokes every listener registered for event, in registration order.

      Type Parameters

      • K extends keyof PeerEvents

      Parameters

      • event: K

        The event name to emit.

      • ...args: PeerEvents[K]

        The argument tuple to pass to each listener, matching Events[event].

      Returns boolean

      true if at least one listener was invoked, false if there were none.

      The listener list is snapshotted before dispatch, so mutations made by handlers take effect only on subsequent emissions. Listeners are isolated: if one throws, the remaining listeners still run and the error is surfaced via console.error rather than swallowed silently (consistent with LocalMessageBus).

    • Reports whether the peer has ponged recently enough to be considered live.

      Parameters

      • deadline: number

        Cutoff timestamp in epoch milliseconds; the peer is alive if its last pong is at or after this value.

      Returns boolean

      true if the peer is still considered connected.

    • Returns the number of listeners currently registered for event.

      Type Parameters

      • K extends keyof PeerEvents

      Parameters

      • event: K

        The event name to count listeners for.

      Returns number

      The count of registered listeners (0 if none).

    • Removes a previously registered listener for event.

      Type Parameters

      • K extends keyof PeerEvents

      Parameters

      • event: K

        The event name the listener was registered for.

      • listener: (...args: PeerEvents[K]) => void

        The exact function reference passed to on or once.

      Returns this

      This emitter, for chaining.

      If the listener is not currently registered, this is a no-op. Only the first matching registration is removed.

    • Registers a listener that is invoked every time event is emitted.

      Type Parameters

      • K extends keyof PeerEvents

      Parameters

      • event: K

        The event name to listen for.

      • listener: (...args: PeerEvents[K]) => void

        Callback invoked with the event's argument tuple.

      Returns this

      This emitter, for chaining.

    • Registers a listener that is invoked at most once: it is removed automatically before being called the first time event is emitted.

      Type Parameters

      • K extends keyof PeerEvents

      Parameters

      • event: K

        The event name to listen for.

      • listener: (...args: PeerEvents[K]) => void

        Callback invoked with the event's argument tuple on the next emission.

      Returns this

      This emitter, for chaining.

      Remove a pending one-time listener by passing the same function reference to off.

    • Removes listeners for a single event, or for all events.

      Parameters

      • Optionalevent: keyof PeerEvents

        The event name to clear; if omitted, listeners for every event are removed.

      Returns this

      This emitter, for chaining.

    • Serializes and sends a message to the peer over its WebSocket.

      Parameters

      • msg:
            | {
                iceServers?: {
                    credential?: string;
                    urls: string
                    | string[];
                    username?: string;
                }[];
                localRole?: string;
                peerId: string;
                peerMetadata?: Record<string, Record<string, string>>;
                peerRoles?: Record<string, string>;
                peers: string[];
                roomId: string;
                type: "room-joined";
                v?: number;
            }
            | {
                metadata?: Record<string, string>;
                peerId: string;
                role?: string;
                type: "peer-joined";
            }
            | { peerId: string; type: "peer-left" }
            | { peerId: string; type: "presence-online" }
            | { peerId: string; type: "presence-offline" }
            | { peerId: string; reason?: string; type: "kicked" }
            | { data: unknown; from: string; type: "signal" }
            | {
                channel: string;
                data?: unknown;
                from: string;
                ts: number;
                type: "broadcast";
            }
            | { code: string; message: string; type: "error" }
            | { type: "ping" }
            | { peerId: string; role: string; type: "role-changed" }

        The server message to send.

        • {
              iceServers?: {
                  credential?: string;
                  urls: string | string[];
                  username?: string;
              }[];
              localRole?: string;
              peerId: string;
              peerMetadata?: Record<string, Record<string, string>>;
              peerRoles?: Record<string, string>;
              peers: string[];
              roomId: string;
              type: "room-joined";
              v?: number;
          }
          • OptionaliceServers?: { credential?: string; urls: string | string[]; username?: string }[]
          • OptionallocalRole?: string
          • peerId: string
          • OptionalpeerMetadata?: Record<string, Record<string, string>>
          • OptionalpeerRoles?: Record<string, string>
          • peers: string[]
          • roomId: string
          • type: "room-joined"
          • Optionalv?: number

            Server's signaling protocol version; see PROTOCOL_VERSION. Optional for back-compat.

        • {
              metadata?: Record<string, string>;
              peerId: string;
              role?: string;
              type: "peer-joined";
          }
        • { peerId: string; type: "peer-left" }
        • { peerId: string; type: "presence-online" }
        • { peerId: string; type: "presence-offline" }
        • { peerId: string; reason?: string; type: "kicked" }
        • { data: unknown; from: string; type: "signal" }
        • { channel: string; data?: unknown; from: string; ts: number; type: "broadcast" }
        • { code: string; message: string; type: "error" }
        • { type: "ping" }
        • { peerId: string; role: string; type: "role-changed" }

      Returns void

      If the WebSocket is not open, or if the underlying send fails (which also emits PeerEvent.Error).

    • Sets the peer's role.

      Parameters

      • role: string

        The new role string.

      Returns void

      Updates local state only; use Room.setPeerRole to also notify the rest of the room.