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

    Browser P2P wrapper around a single RTCPeerConnection, implementing the "perfect negotiation" pattern: it debounces negotiationneeded, resolves offer collisions using the polite role, and buffers ICE candidates that arrive before the remote description is set. It emits ConnectionEvents rather than exposing the raw connection.

    This is the per-peer building block driven by Call; applications typically use Call instead of constructing this directly.

    Hierarchy (View Summary)

    Index

    Constructors

    • Parameters

      • polite: boolean

        Whether this side is the "polite" peer. On an offer collision the polite peer rolls back its local offer and accepts the remote one, while the impolite peer ignores the remote offer.

      • opts: CallOptions = {}

        Connection options (ICE servers, codec preference, simulcast, candidate filter, peer connection factory). Defaults to {}.

      Returns PeerConnection

    Methods

    • Adds a remote ICE candidate. If the remote description is not yet set, the candidate is buffered (up to an internal cap) and applied once it is; a null candidate signals end-of-candidates.

      Parameters

      • candidate: RTCIceCandidateInit | null

        The remote ICE candidate, or null for end-of-candidates.

      Returns Promise<void>

      Emits ConnectionEvent.Error if the pending-candidate buffer overflows.

    • Adds a local track to the connection, triggering renegotiation. When simulcast layers or a maxBitrate are configured, a transceiver with the corresponding send encodings is created; otherwise a plain sender is added. Any configured codec preference is then applied.

      Parameters

      • track: MediaStreamTrack

        The local media track to send.

      • stream: MediaStream

        The stream the track belongs to.

      • OptionalmaxBitrate: number

        Optional maximum send bitrate, in bits per second (ignored when simulcast layers are configured).

      Returns RTCRtpSender

      The RTCRtpSender for the added track.

    • Creates a locally initiated RTCDataChannel on this connection.

      Parameters

      • label: string

        Channel label.

      • Optionalopts: RTCDataChannelInit

        Optional data channel configuration (ordering, reliability).

      Returns RTCDataChannel

      The created data channel.

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

      Type Parameters

      • K extends keyof PeerConnectionEvents

      Parameters

      • event: K

        The event name to emit.

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

    • Retrieves connection statistics.

      Returns Promise<RTCStatsReport>

      The RTCStatsReport for this connection.

    • Applies a remote SDP answer to a previously sent offer, then flushes any buffered ICE candidates. Emits ConnectionEvent.Error on failure.

      Parameters

      • sdp: string

        The remote answer SDP.

      Returns Promise<void>

    • Applies a remote SDP offer and produces an answer, handling offer collisions per the perfect-negotiation algorithm.

      Parameters

      • sdp: string

        The remote offer SDP.

      Returns Promise<RTCSessionDescriptionInit | null>

      The local answer description to send back, or null if the offer was ignored (impolite peer during a collision) or an error occurred (an ConnectionEvent.Error is emitted in the error case).

    • Returns the number of listeners currently registered for event.

      Type Parameters

      • K extends keyof PeerConnectionEvents

      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 PeerConnectionEvents

      Parameters

      • event: K

        The event name the listener was registered for.

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

      Parameters

      • event: K

        The event name to listen for.

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

      Parameters

      • event: K

        The event name to listen for.

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

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

      Returns this

      This emitter, for chaining.

    • Restarts ICE, forcing a fresh connectivity check. Useful after a network change to recover a connection.

      Returns void