peerix - v0.5.0
    Preparing search index...

    Class RemotePeer

    Represents a remote peer connection. Do not create RemotePeer instances manually.

    Index

    Accessors

    • get channels(): Map<string, RTCDataChannel>

      Negotiated data channels keyed by channel label.

      Returns Map<string, RTCDataChannel>

    • get connection(): RTCPeerConnection

      Native WebRTC peer connection to the remote peer.

      Returns RTCPeerConnection

    • get id(): string

      Remote peer identifier.

      Returns string

    • get metadata(): unknown

      Metadata advertised by the remote peer.

      Returns unknown

    • get room(): string

      Room name the peer is associated with.

      Returns string

    • get streams(): Map<string, MediaStream>

      Remote media streams keyed by stream label.

      Returns Map<string, MediaStream>

    Methods

    • Closes a previously opened data channel to the current remote peer.

      Parameters

      • options: string | { label: string }

        Channel label or object containing label.

      Returns Promise<void>

      // close the channel with label "chat"
      await remote.close({ label: "chat" });
    • Closes and frees all connection resources.

      Returns void

    • Unregisters an event handler for a specific event type emitted by the remote peer connection.

      Type Parameters

      Parameters

      • event: K | K[]

        Event type to stop listening for.

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

        Callback function to remove from the event listeners.

      Returns void

    • Registers an event handler for a specific event type emitted by the remote peer connection.

      Type Parameters

      Parameters

      Returns void

      // subscribe to the "connection" event
      peer.on("connection", (e) => {
      console.log("Connection state is changed:", e.state);
      });

      @param event Event type to listen for. @param handler Callback function to handle the event.

    • Opens a data channel to the current remote peer.

      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"
      await remote.open({ label: "chat" });
    • Sends a message through a data channel.

      If options is a string, it is treated as the channel label. If no label is provided, it uses the default channel.

      The send method works only with open channels that have no protocol specified, are ordered (reliable), and match the specified label.

      Parameters

      • message: unknown

        Message payload to send.

      • Optionaloptions: string | SendOptions

        Send options or channel label.

      Returns ReadableStream<TransferProgress> & Promise<void>

      A ReadableStream of transfer progress status or a Promise.

      // send a message to default channel
      await remote.send("Hello, peer!");
      // send large data with a progress handler
      const file = new File([new Uint8Array(1024 * 1024)], "example.dat");
      const transfer = remote.send(file, {
      label: "chat", // channel label
      info: { filename: file.name }, // metadata
      signal: AbortSignal.timeout(10000), // abort signal
      });
      // optionally handle the progress
      for await (const progress of transfer) {
      const { id, label, current, total } = progress;
      const percent = Math.round((current / total) * 100);
      console.log(`[${id}:${label}] Sending... ${percent}%`);
      }
    • Shares a new media stream to the current remote peer or updates an existing one.

      If you pass a MediaStream instance directly, it will be shared 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 shared with the managed option, its tracks will be automatically stopped when the stream is unshared or replaced with a new stream.

      Parameters

      • options: MediaStream | StreamOptions

        Stream descriptor or MediaStream instance.

      Returns Promise<void | MediaStream>

      The shared 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 });

      // share a media stream with an explicit label
      const sharedStream = await remote.share({ label: "camera", stream, managed: true });
    • Serializes the remote peer to a JSON-compatible object.

      Returns {
          channels: string[];
          id: string;
          metadata: unknown;
          room: string;
          state: PeerConnectionState;
          streams: string[];
      }

      A serializable representation of the peer.

    • Stops sharing a previously shared media stream to the current remote peer.

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

      If the stream was shared 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 unshared MediaStream instance, or undefined.

      // unshare a media stream with an explicit label
      await remote.unshare({ label: "camera" });