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

    Class WebSocketTransport

    Default WebSocket-based Transport.

    Handles the full connection lifecycle: it resolves a WebSocket implementation (the global one in browsers/Node ≥ 22, falling back to the ws package), validates every inbound frame against ServerMessageSchema and silently drops invalid ones, and answers server ping frames with pong automatically. Messages sent while the socket is not open are buffered in a MessageQueue and flushed on reconnect. When TransportOptions.reconnect is enabled it retries using a BackoffStrategy, optionally refreshing the auth token via TransportOptions.tokenRefresh before each attempt, and gives up once the strategy is exhausted.

    const transport = new WebSocketTransport('wss://example.com/rtc?roomId=demo', {
    reconnect: true,
    maxReconnectDelay: 32_000,
    })
    transport.on('message', (msg) => console.log(msg))
    await transport.connect()

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Methods

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

      Type Parameters

      Parameters

      • event: K

        The event name to emit.

      • ...args: TransportEvents[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).

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

      Type Parameters

      Parameters

      • event: K

        The event name to listen for.

      • listener: (...args: TransportEvents[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 TransportEvents

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

      Returns this

      This emitter, for chaining.

    • Sends a message immediately if connected, otherwise buffers it in the send queue.

      Parameters

      • msg:
            | { data: unknown; to: string; type: "signal" }
            | { type: "pong" }
            | { channel: string; data?: unknown; type: "broadcast" }

      Returns void