API Reference
    Preparing search index...

    Class Peer

    Peer class for managing WebRTC peer connections and signaling.

    // create a new peer
    // using default in-memory signaling driver
    const peer = new Peer();

    // listen for open channel event
    peer.on('open', (e) => {
    const { remote, channel } = e;
    // send a message to the connected peer
    channel.send('Hello, peer!');
    });

    // listen for incoming messages
    peer.on('message', (e) => {
    const { remote, channel, data } = e;
    console.log('Received message:', data);
    });

    // open a data channel
    peer.open({ id: 0 });

    // join a room
    peer.join('room-id');
    Index

    Constructors

    Properties

    addons: Set<any>

    Attachable extensions.

    channels: Map<number, ChannelOptions>

    Configured local data channels indexed by channel id.

    connections: Map<string, RemotePeer>

    Active remote peers indexed by remote peer id.

    connectionTimeout: number

    Maximum time in seconds to wait for ICE connection establishment.

    Signaling transport used to exchange SDP and ICE messages.

    iceServers: { credential?: string; urls: string | string[]; username?: string }[]

    STUN/TURN servers passed to every RTCPeerConnection instance.

    iceTransportPolicy: "all" | "relay"

    ICE transport policy for created peer connections.

    id: string

    Unique identifier of the local peer.

    metadata: any

    Optional metadata announced to other peers in signaling messages.

    room: string

    Current room name. Empty until join() is called.

    streams: Map<string | number, StreamOptions>

    Published local streams indexed by application-level stream id.

    Accessors

    • get active(): boolean

      Indicates whether the peer is currently active.

      Returns boolean

      True if the Peer is joined to a room, false otherwise.

    Methods

    • Attach an addon/extension to the peer instance.

      Parameters

      • addon: any

        Addon instance to attach.

      Returns Promise<void>

    • Close and unregister a negotiated data channel by id.

      Parameters

      • options: number | { id: number }

        Channel id or object containing id.

      Returns void

    • Detach a previously attached addon/extension from the peer instance.

      Parameters

      • addon: any

        Addon instance to detach.

      Returns Promise<void>

    • Join a room and start listening for incoming connections.

      Parameters

      • Optionaloptions: string | JoinOptions

        Room name or join options.

      Returns void

    • Leave the current room and close all active remote connections.

      Returns void

    • Remove a previously registered event listener.

      Type Parameters

      Parameters

      • event: K | K[]

        Event name or list of event names.

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

        Event handler to remove. If omitted, all handlers for the given event(s) will be removed.

      Returns void

    • Subscribe to one or more peer events.

      Type Parameters

      Parameters

      • event: K | K[]

        Event name or list of event names.

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

        Event handler.

      Returns void

    • Subscribe to an event and auto-unsubscribe after first invocation.

      Type Parameters

      Parameters

      • event: K | K[]

        Event name or list of event names.

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

        Event handler.

      Returns void

    • Register or create a negotiated data channel with all remote peers.

      Parameters

      Returns void

    • Publish or update a local media stream.

      When already active, this updates senders on every current connection and triggers negotiation where applicable.

      Parameters

      • options: MediaStream | StreamOptions

        Stream descriptor or MediaStream instance.

      Returns void

    • Send a message through data channels.

      If options is omitted, the message is sent to all open channels for every connected remote peer. If options is a number, it is treated as channel id.

      Parameters

      • message: any

        Message payload to send. This may be a string, a Blob, an ArrayBuffer, a TypedArray or a DataView object.

      • Optionaloptions: number | SendOptions

        Optional send options or channel id.

      Returns void

    • Stop publishing a previously published local stream.

      Parameters

      • options: string | number | { id: string | number }

        Stream identifier or object containing id.

      Returns void