peerjs

  • Version 1.3.2
  • Published
  • 1.51 MB
  • 4 dependencies
  • MIT license

Install

npm i peerjs
yarn add peerjs
pnpm add peerjs

Overview

PeerJS client

Index

Classes

class Peer

class Peer {}

    constructor

    constructor(id?: string, options?: Peer.PeerJSOption);
    • A peer can connect to other peers and listen for connections.

      Parameter id

      Other peers can connect to this peer using the provided ID. If no ID is given, one will be generated by the brokering server.

      Parameter options

      for specifying details about PeerServer

    constructor

    constructor(options: Peer.PeerJSOption);
    • A peer can connect to other peers and listen for connections.

      Parameter options

      for specifying details about PeerServer

    property connections

    connections: any;
    • A hash of all connections associated with this peer, keyed by the remote peer's ID.

    property destroyed

    destroyed: boolean;
    • true if this peer and all of its connections can no longer be used.

    property disconnected

    disconnected: boolean;
    • false if there is an active connection to the PeerServer.

    property id

    id: string;
    • The brokering ID of this peer

    property prototype

    prototype: RTCIceServer;

      method call

      call: (
      id: string,
      stream: MediaStream,
      options?: Peer.CallOption
      ) => Peer.MediaConnection;
      • Calls the remote peer specified by id and returns a media connection.

        Parameter id

        The brokering ID of the remote peer (their peer.id).

        Parameter stream

        The caller's media stream

        Parameter options

        Metadata associated with the connection, passed in by whoever initiated the connection.

      method connect

      connect: (id: string, options?: Peer.PeerConnectOption) => Peer.DataConnection;
      • Connects to the remote peer specified by id and returns a data connection.

        Parameter id

        The brokering ID of the remote peer (their peer.id).

        Parameter options

        for specifying details about Peer Connection

      method destroy

      destroy: () => void;
      • Close the connection to the server and terminate all existing connections.

      method disconnect

      disconnect: () => void;
      • Close the connection to the server, leaving all existing data and media connections intact.

      method getConnection

      getConnection: (
      peerId: string,
      connectionId: string
      ) => Peer.MediaConnection | Peer.DataConnection | null;
      • Retrieve a data/media connection for this peer.

        Parameter peerId

        Parameter connectionId

      method listAllPeers

      listAllPeers: (callback: (peerIds: Array<string>) => void) => void;
      • Get a list of available peer IDs

        Parameter callback

      method off

      off: (event: string, fn: Function, once?: boolean) => void;
      • Remove event listeners.(EventEmitter3)

        Parameter event

        The event we want to remove.

        Parameter fn

        The listener that we need to find.

        Parameter once

        Only remove once listeners.

      method on

      on: {
      (event: string, cb: () => void): void;
      (event: 'open', cb: (id: string) => void): void;
      (
      event: 'connection',
      cb: (dataConnection: Peer.DataConnection) => void
      ): void;
      (event: 'call', cb: (mediaConnection: Peer.MediaConnection) => void): void;
      (event: 'close', cb: () => void): void;
      (event: 'disconnected', cb: () => void): void;
      (event: 'error', cb: (err: any) => void): void;
      };
      • Set listeners for peer events.

        Parameter event

        Event name

        Parameter cb

        Callback Function

      • Emitted when a connection to the PeerServer is established.

        Parameter event

        Event name

        Parameter cb

        id is the brokering ID of the peer

      • Emitted when a new data connection is established from a remote peer.

        Parameter event

        Event name

        Parameter cb

        Callback Function

      • Emitted when a remote peer attempts to call you.

        Parameter event

        Event name

        Parameter cb

        Callback Function

      • Emitted when the peer is destroyed and can no longer accept or create any new connections.

        Parameter event

        Event name

        Parameter cb

        Callback Function

      • Emitted when the peer is disconnected from the signalling server

        Parameter event

        Event name

        Parameter cb

        Callback Function

      • Errors on the peer are almost always fatal and will destroy the peer.

        Parameter event

        Event name

        Parameter cb

        Callback Function

      method reconnect

      reconnect: () => void;
      • Attempt to reconnect to the server with the peer's old ID

      Interfaces

      interface AnswerOption

      interface AnswerOption {}

        property sdpTransform

        sdpTransform?: Function;

          interface CallOption

          interface CallOption {}

            property metadata

            metadata?: any;

              property sdpTransform

              sdpTransform?: Function;

                interface DataConnection

                interface DataConnection {}

                  property bufferSize

                  bufferSize: number;

                    property dataChannel

                    dataChannel: RTCDataChannel;

                      property label

                      label: string;

                        property metadata

                        metadata: any;

                          property open

                          open: boolean;

                            property parse

                            parse: (data: string) => any;

                              property peer

                              peer: string;

                                property peerConnection

                                peerConnection: RTCPeerConnection;

                                  property reliable

                                  reliable: boolean;

                                    property serialization

                                    serialization: string;

                                      property stringify

                                      stringify: (data: any) => string;

                                        property type

                                        type: string;

                                          method close

                                          close: () => void;

                                            method off

                                            off: (event: string, fn: Function, once?: boolean) => void;

                                              method on

                                              on: {
                                              (event: string, cb: () => void): void;
                                              (event: 'data', cb: (data: any) => void): void;
                                              (event: 'open', cb: () => void): void;
                                              (event: 'close', cb: () => void): void;
                                              (event: 'error', cb: (err: any) => void): void;
                                              };

                                                method send

                                                send: (data: any) => void;

                                                  interface MediaConnection

                                                  interface MediaConnection {}

                                                    property metadata

                                                    metadata: any;

                                                      property open

                                                      open: boolean;

                                                        property peer

                                                        peer: string;

                                                          property peerConnection

                                                          peerConnection: RTCPeerConnection;

                                                            property type

                                                            type: string;

                                                              method answer

                                                              answer: (stream?: MediaStream, options?: AnswerOption) => void;

                                                                method close

                                                                close: () => void;

                                                                  method off

                                                                  off: (event: string, fn: Function, once?: boolean) => void;

                                                                    method on

                                                                    on: {
                                                                    (event: string, cb: () => void): void;
                                                                    (event: 'stream', cb: (stream: MediaStream) => void): void;
                                                                    (event: 'close', cb: () => void): void;
                                                                    (event: 'error', cb: (err: any) => void): void;
                                                                    };

                                                                      interface PeerConnectOption

                                                                      interface PeerConnectOption {}

                                                                        property label

                                                                        label?: string;

                                                                          property metadata

                                                                          metadata?: any;

                                                                            property reliable

                                                                            reliable?: boolean;

                                                                              property serialization

                                                                              serialization?: string;

                                                                                interface PeerJSOption

                                                                                interface PeerJSOption {}

                                                                                  property config

                                                                                  config?: RTCConfiguration;

                                                                                    property debug

                                                                                    debug?: number;

                                                                                      property host

                                                                                      host?: string;

                                                                                        property key

                                                                                        key?: string;

                                                                                          property path

                                                                                          path?: string;

                                                                                            property port

                                                                                            port?: number;

                                                                                              property secure

                                                                                              secure?: boolean;

                                                                                                interface util

                                                                                                interface util {}

                                                                                                  property browser

                                                                                                  browser: string;

                                                                                                    property supports

                                                                                                    supports: UtilSupportsObj;

                                                                                                      interface UtilSupportsObj

                                                                                                      interface UtilSupportsObj {}

                                                                                                        property audioVideo

                                                                                                        audioVideo: boolean;

                                                                                                          property binaryBlob

                                                                                                          binaryBlob: boolean;

                                                                                                            property browser

                                                                                                            browser: boolean;

                                                                                                              property data

                                                                                                              data: boolean;

                                                                                                                property reliable

                                                                                                                reliable: boolean;

                                                                                                                  property webRTC

                                                                                                                  webRTC: boolean;

                                                                                                                    Package Files (1)

                                                                                                                    Dependencies (4)

                                                                                                                    Dev Dependencies (11)

                                                                                                                    Peer Dependencies (0)

                                                                                                                    No peer dependencies.

                                                                                                                    Badge

                                                                                                                    To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                                    You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/peerjs.

                                                                                                                    • Markdown
                                                                                                                      [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/peerjs)
                                                                                                                    • HTML
                                                                                                                      <a href="https://www.jsdocs.io/package/peerjs"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>