peerix - v0.1.0
    Preparing search index...

    Class Peer

    Manages WebRTC peer connections, signaling, media streams, and data channels.

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

    // listen for connection state changes
    peer.on('connection', (e) => {
    const { remote } = e;
    console.log(`Peer ${remote.id} connection state has changed:`, remote.state);
    });

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

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

    // open a data channel
    peer.open({ label: 'default' });

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

    Constructors

    • Creates a new Peer instance.

      Parameters

      • Optionaloptions: PeerOptions

        Peer configuration options.

      Returns Peer

      // create a new peer with default options
      const peer = new Peer();

    Properties

    active: boolean

    Indicates whether the peer is currently active (joined a room).

    addons: Set<any>

    Attachable extensions.

    channels: Map<string, ChannelOptions>

    Configured local data channels indexed by channel label.

    connections: Map<string, RemotePeer>

    Active remote peers indexed by remote peer id.

    id: string

    Unique identifier for 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, StreamOptions>

    Published local streams indexed by application-level stream label.

    Methods

    • Attaches an addon/extension to the peer instance.

      Parameters

      • addon: any

        Addon instance to attach.

      Returns Promise<void>

    • Closes a previously opened data channel with the given label and removes it from all remote peers.

      Parameters

      • options: string | { label: string }

        Channel label or object containing label.

      Returns Promise<void>

      // close the channel with label 'chat'
      peer.close({ label: 'chat' });
    • Detaches a previously attached addon/extension from the peer instance.

      Parameters

      • addon: any

        Addon instance to detach.

      Returns Promise<void>

    • Emits one or more events. Usually you would not call this method directly.

      Type Parameters

      Parameters

      • event: K | K[]

        Event name or list of event names.

      • ...args: PeerEvents[K]

        Event payload.

      Returns void

    • Joins a room and starts listening for incoming connections.

      Parameters

      Returns Promise<void>

      // join a room with ID 'room-id' and custom metadata
      peer.join({ room: 'room-id', metadata: { name: 'Alice' } });
    • Leaves the current room and closes all active remote connections.

      Returns Promise<void>

      // leave the current room
      peer.leave();
    • Removes 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

    • Subscribes 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

    • Subscribes to an event and auto-unsubscribes 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

    • Opens a data channel with the given label and options to all remote peers. If a channel with the same label already exists, it will be reused.

      You can open a channel with the same label on both local and remote peers or only on one side. In any case, only one channel will be created for each label. You can send data through the channel in both directions.

      Parameters

      Returns Promise<void>

      // open a channel with label 'chat'
      peer.open({ label: 'chat' });
    • Publishes new or updates an existing media stream to all remote peers including new ones that join later.

      If you pass a MediaStream instance directly, it will be published under a label equal to the stream id. Otherwise, you can specify an explicit label in the options object. If a stream with the same label already exists, it will be updated and its tracks will be added/removed as needed to minimize renegotiations.

      If the stream is published with the managed option, its tracks will be automatically stopped when the stream is unpublished or replaced with a new stream.

      Parameters

      • options: MediaStream | StreamOptions

        Stream descriptor or MediaStream instance.

      Returns Promise<void | MediaStream>

      The published MediaStream instance if successful, or undefined.

      // get a media stream from the user's camera and microphone
      const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });

      // publish a media stream with an explicit label
      peer.publish({ label: 'camera', stream, managed: true });
    • Sends 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 string, it is treated as channel label.

      Parameters

      • message: any

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

      • Optionaloptions: string | { label?: string }

        Optional channel label or object containing label.

      Returns void

      // send a message to all channels
      peer.send('Hello, peers!');
      // send a message to a specific channel
      peer.send('Hello, chat channel!', { label: 'chat' });
    • Stops publishing a previously published media stream with the given label and removes it from all remote peers.

      If you pass a MediaStream instance directly, it will be unpublished based on its id as label. Otherwise, you can specify the label in the options object or pass it directly as a string.

      If the stream was published with the managed option, its tracks will be stopped automatically.

      Parameters

      • options: string | MediaStream | { label?: string }

        A stream label, MediaStream instance, or an object containing a label.

      Returns Promise<void | MediaStream>

      The unpublished MediaStream instance, or undefined.

      // unpublish a media stream with an explicit label
      peer.unpublish({ label: 'camera' });