peerjs

  • Version 1.5.4
  • Published
  • 2.17 MB
  • 4 dependencies
  • MIT license

Install

npm i peerjs
yarn add peerjs
pnpm add peerjs

Overview

PeerJS client

Index

Variables

variable util

const util: Util;
  • Provides a variety of helpful utilities.

    :::caution Only the utilities documented here are guaranteed to be present on util. Undocumented utilities can be removed without warning. We don't consider these to be breaking changes. :::

Classes

class BufferedConnection

abstract class BufferedConnection extends DataConnection {}

    property bufferSize

    readonly bufferSize: number;

      method close

      close: (options?: { flush?: boolean }) => void;

        class DataConnection

        abstract class DataConnection extends BaseConnection<
        DataConnectionEvents,
        DataConnectionErrorType
        > {}
        • Wraps a DataChannel between two Peers.

        constructor

        constructor(peerId: string, provider: Peer, options: any);

          property ID_PREFIX

          protected static readonly ID_PREFIX: string;

            property MAX_BUFFERED_AMOUNT

            protected static readonly MAX_BUFFERED_AMOUNT: number;

              property reliable

              readonly reliable: boolean;

                property serialization

                abstract readonly serialization: string;

                  property type

                  readonly type: ConnectionType;

                    method close

                    close: (options?: { flush?: boolean }) => void;
                    • Allows user to close connection.

                    method handleMessage

                    handleMessage: (message: ServerMessage) => Promise<void>;

                      method send

                      send: (data: any, chunked?: boolean) => void | Promise<void>;
                      • Allows user to send data.

                      class MediaConnection

                      class MediaConnection extends BaseConnection<MediaConnectionEvents> {}
                      • Wraps WebRTC's media streams. To get one, use or listen for the event.

                      constructor

                      constructor(peerId: string, provider: Peer, options: any);

                        property label

                        readonly label: string;

                          property localStream

                          readonly localStream: MediaStream;

                            property remoteStream

                            readonly remoteStream: MediaStream;

                              property type

                              readonly type: ConnectionType;
                              • For media connections, this is always 'media'.

                              method addStream

                              addStream: (remoteStream: any) => void;

                                method answer

                                answer: (stream?: MediaStream, options?: AnswerOption) => void;
                                • When receiving a event on a peer, you can call answer on the media connection provided by the callback to accept the call and optionally send your own media stream.

                                  Parameter stream

                                  A WebRTC media stream.

                                  Parameter options

                                  Returns

                                method close

                                close: () => void;
                                • Closes the media connection.

                                class MsgPack

                                class MsgPack extends StreamConnection {}

                                  constructor

                                  constructor(peerId: string, provider: Peer, options: any);

                                    property serialization

                                    readonly serialization: string;

                                      class MsgPackPeer

                                      class MsgPackPeer extends Peer {}
                                      • Modifiers

                                        • @experimental

                                      class Peer

                                      class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {}
                                      • A peer who can initiate connections with other peers.

                                      constructor

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

                                      constructor

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

                                        Parameter options

                                        for specifying details about PeerServer

                                      constructor

                                      constructor(id: string, options?: PeerOptions);
                                      • 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. The ID must start and end with an alphanumeric character (lower or upper case character or a digit). In the middle of the ID spaces, dashes (-) and underscores (_) are allowed. Use to send identifying information.

                                        Parameter options

                                        for specifying details about PeerServer

                                      property connections

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

                                        Deprecated

                                        Return type will change from Object to Map<string,[]>

                                      property destroyed

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

                                      property disconnected

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

                                      property id

                                      readonly id: string;
                                      • The brokering ID of this peer

                                        If no ID was specified in , this will be undefined until the event is emitted.

                                      property open

                                      readonly open: boolean;

                                        property options

                                        readonly options: PeerOptions;

                                          method call

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

                                            Parameter peer

                                            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: (peer: string, options?: PeerConnectOption) => DataConnection;
                                          • Connects to the remote peer specified by id and returns a data connection.

                                            Parameter peer

                                            The brokering ID of the remote peer (their ).

                                            Parameter options

                                            for specifying details about Peer Connection

                                          method destroy

                                          destroy: () => void;
                                          • Destroys the Peer: closes all active connections as well as the connection to the server.

                                            :::caution This cannot be undone; the respective peer object will no longer be able to create or receive any connections, its ID will be forfeited on the server, and all of its data and media connections will be closed. :::

                                          method disconnect

                                          disconnect: () => void;
                                          • Disconnects the Peer's connection to the PeerServer. Does not close any active connections. Warning: The peer can no longer create or accept connections after being disconnected. It also cannot reconnect to the server.

                                          method getConnection

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

                                          method listAllPeers

                                          listAllPeers: (cb?: (_: any[]) => void) => void;
                                          • Get a list of available peer IDs. If you're running your own server, you'll want to set allow_discovery: true in the PeerServer options. If you're using the cloud server, email team@peerjs.com to get the functionality enabled for your key.

                                          method reconnect

                                          reconnect: () => void;
                                          • Attempts to reconnect with the same ID.

                                            Only can be reconnected. Destroyed peers cannot be reconnected. If the connection fails (as an example, if the peer's old ID is now taken), the peer's existing connections will not close, but any associated errors events will fire.

                                          class PeerError

                                          class PeerError<T extends string> extends Error {}
                                          • A PeerError is emitted whenever an error occurs. It always has a .type, which can be used to identify the error.

                                          property type

                                          type: string;

                                            class PeerOptions

                                            class PeerOptions implements PeerJSOption {}

                                              property config

                                              config?: any;
                                              • Configuration hash passed to RTCPeerConnection. This hash contains any custom ICE/TURN server configuration.

                                                Defaults to

                                              property debug

                                              debug?: LogLevel;
                                              • Prints log messages depending on the debug level passed in.

                                              property host

                                              host?: string;
                                              • Server host. Defaults to 0.peerjs.com. Also accepts '/' to signify relative hostname.

                                              property key

                                              key?: string;
                                              • API key for the PeerServer. This is not used anymore.

                                                Deprecated

                                              property logFunction

                                              logFunction?: (logLevel: LogLevel, ...rest: any[]) => void;

                                                property path

                                                path?: string;
                                                • The path where your self-hosted PeerServer is running. Defaults to '/'

                                                property pingInterval

                                                pingInterval?: number;

                                                  property port

                                                  port?: number;
                                                  • Server port. Defaults to 443.

                                                  property referrerPolicy

                                                  referrerPolicy?: ReferrerPolicy;

                                                    property secure

                                                    secure?: boolean;
                                                    • Set to true true if you're using TLS. :::danger If possible *always use TLS* :::

                                                    property serializers

                                                    serializers?: SerializerMapping;

                                                      property token

                                                      token?: string;

                                                        class StreamConnection

                                                        abstract class StreamConnection extends DataConnection {}

                                                          constructor

                                                          protected constructor(peerId: string, provider: Peer, options: any);

                                                            property writer

                                                            protected writer: WritableStreamDefaultWriter<Uint8Array>;

                                                              class Util

                                                              class Util extends BinaryPackChunker {}

                                                                property browser

                                                                readonly browser: string;

                                                                  property browserVersion

                                                                  readonly browserVersion: number;

                                                                    property chunkedBrowsers

                                                                    readonly chunkedBrowsers: { Chrome: number; chrome: number };

                                                                      property CLOUD_HOST

                                                                      readonly CLOUD_HOST: string;

                                                                        property CLOUD_PORT

                                                                        readonly CLOUD_PORT: number;

                                                                          property defaultConfig

                                                                          readonly defaultConfig: {
                                                                          iceServers: (
                                                                          | { urls: string; username?: undefined; credential?: undefined }
                                                                          | { urls: string[]; username: string; credential: string }
                                                                          )[];
                                                                          sdpSemantics: string;
                                                                          };

                                                                            property pack

                                                                            pack: any;

                                                                              property randomToken

                                                                              randomToken: () => string;

                                                                                property supports

                                                                                readonly supports: UtilSupportsObj;
                                                                                • A hash of WebRTC features mapped to booleans that correspond to whether the feature is supported by the current browser.

                                                                                  :::caution Only the properties documented here are guaranteed to be present on util.supports :::

                                                                                property unpack

                                                                                unpack: any;

                                                                                  property validateId

                                                                                  validateId: (id: string) => boolean;

                                                                                    method binaryStringToArrayBuffer

                                                                                    binaryStringToArrayBuffer: (binary: string) => ArrayBuffer | SharedArrayBuffer;

                                                                                      method blobToArrayBuffer

                                                                                      blobToArrayBuffer: (
                                                                                      blob: Blob,
                                                                                      cb: (arg: ArrayBuffer | null) => void
                                                                                      ) => FileReader;

                                                                                        method isSecure

                                                                                        isSecure: () => boolean;

                                                                                          method noop

                                                                                          noop: () => void;

                                                                                            Interfaces

                                                                                            interface AnswerOption

                                                                                            interface AnswerOption {}

                                                                                              property sdpTransform

                                                                                              sdpTransform?: Function;
                                                                                              • Function which runs before create answer to modify sdp answer message.

                                                                                              interface CallOption

                                                                                              interface CallOption {}

                                                                                                property metadata

                                                                                                metadata?: any;
                                                                                                • Metadata associated with the connection, passed in by whoever initiated the connection.

                                                                                                  Can be accessed with . Can be any serializable type.

                                                                                                property sdpTransform

                                                                                                sdpTransform?: Function;
                                                                                                • Function which runs before create offer to modify sdp offer message.

                                                                                                interface PeerConnectOption

                                                                                                interface PeerConnectOption {}

                                                                                                  property label

                                                                                                  label?: string;
                                                                                                  • A unique label by which you want to identify this data connection. If left unspecified, a label will be generated at random.

                                                                                                    Can be accessed with

                                                                                                  property metadata

                                                                                                  metadata?: any;
                                                                                                  • Metadata associated with the connection, passed in by whoever initiated the connection.

                                                                                                    Can be accessed with . Can be any serializable type.

                                                                                                  property reliable

                                                                                                  reliable?: boolean;

                                                                                                    property serialization

                                                                                                    serialization?: string;

                                                                                                      interface PeerEvents

                                                                                                      interface PeerEvents {}

                                                                                                        property call

                                                                                                        call: (mediaConnection: MediaConnection) => void;
                                                                                                        • Emitted when a remote peer attempts to call you.

                                                                                                        property close

                                                                                                        close: () => void;
                                                                                                        • Emitted when the peer is destroyed and can no longer accept or create any new connections.

                                                                                                        property connection

                                                                                                        connection: (dataConnection: DataConnection) => void;
                                                                                                        • Emitted when a new data connection is established from a remote peer.

                                                                                                        property disconnected

                                                                                                        disconnected: (currentId: string) => void;
                                                                                                        • Emitted when the peer is disconnected from the signalling server

                                                                                                        property error

                                                                                                        error: (error: PeerError<`${PeerErrorType}`>) => void;
                                                                                                        • Errors on the peer are almost always fatal and will destroy the peer.

                                                                                                          Errors from the underlying socket and PeerConnections are forwarded here.

                                                                                                        property open

                                                                                                        open: (id: string) => void;
                                                                                                        • Emitted when a connection to the PeerServer is established.

                                                                                                          You may use the peer before this is emitted, but messages to the server will be queued. id is the brokering ID of the peer (which was either provided in the constructor or assigned by the server).You should not wait for this event before connecting to other peers if connection speed is important.

                                                                                                        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 referrerPolicy

                                                                                                                      referrerPolicy?: ReferrerPolicy;

                                                                                                                        property secure

                                                                                                                        secure?: boolean;

                                                                                                                          property token

                                                                                                                          token?: string;

                                                                                                                            interface SerializerMapping

                                                                                                                            interface SerializerMapping {}

                                                                                                                              index signature

                                                                                                                              [key: string]: new (
                                                                                                                              peerId: string,
                                                                                                                              provider: Peer,
                                                                                                                              options: any
                                                                                                                              ) => DataConnection;

                                                                                                                                interface UtilSupportsObj

                                                                                                                                interface UtilSupportsObj {}

                                                                                                                                  property audioVideo

                                                                                                                                  audioVideo: boolean;
                                                                                                                                  • True if the current browser supports media streams and PeerConnection.

                                                                                                                                  property binaryBlob

                                                                                                                                  binaryBlob: boolean;

                                                                                                                                    property browser

                                                                                                                                    browser: boolean;
                                                                                                                                    • The current browser. This property can be useful in determining whether two peers can connect.

                                                                                                                                      if (util.browser === 'firefox') {
                                                                                                                                      // OK to peer with Firefox peers.
                                                                                                                                      }

                                                                                                                                      util.browser can currently have the values `'firefox', 'chrome', 'safari', 'edge', 'Not a supported browser.', 'Not a browser.' (unknown WebRTC-compatible agent).

                                                                                                                                    property data

                                                                                                                                    data: boolean;
                                                                                                                                    • True if the current browser supports DataChannel and PeerConnection.

                                                                                                                                    property reliable

                                                                                                                                    reliable: boolean;
                                                                                                                                    • True if the current browser supports reliable DataChannels.

                                                                                                                                    property webRTC

                                                                                                                                    webRTC: boolean;

                                                                                                                                      Enums

                                                                                                                                      enum BaseConnectionErrorType

                                                                                                                                      enum BaseConnectionErrorType {
                                                                                                                                      NegotiationFailed = 'negotiation-failed',
                                                                                                                                      ConnectionClosed = 'connection-closed',
                                                                                                                                      }

                                                                                                                                        member ConnectionClosed

                                                                                                                                        ConnectionClosed = 'connection-closed'

                                                                                                                                          member NegotiationFailed

                                                                                                                                          NegotiationFailed = 'negotiation-failed'

                                                                                                                                            enum ConnectionType

                                                                                                                                            enum ConnectionType {
                                                                                                                                            Data = 'data',
                                                                                                                                            Media = 'media',
                                                                                                                                            }

                                                                                                                                              member Data

                                                                                                                                              Data = 'data'

                                                                                                                                                member Media

                                                                                                                                                Media = 'media'

                                                                                                                                                  enum DataConnectionErrorType

                                                                                                                                                  enum DataConnectionErrorType {
                                                                                                                                                  NotOpenYet = 'not-open-yet',
                                                                                                                                                  MessageToBig = 'message-too-big',
                                                                                                                                                  }

                                                                                                                                                    member MessageToBig

                                                                                                                                                    MessageToBig = 'message-too-big'

                                                                                                                                                      member NotOpenYet

                                                                                                                                                      NotOpenYet = 'not-open-yet'

                                                                                                                                                        enum LogLevel

                                                                                                                                                        enum LogLevel {
                                                                                                                                                        Disabled = 0,
                                                                                                                                                        Errors = 1,
                                                                                                                                                        Warnings = 2,
                                                                                                                                                        All = 3,
                                                                                                                                                        }

                                                                                                                                                          member All

                                                                                                                                                          All = 3
                                                                                                                                                          • Prints all logs.

                                                                                                                                                          member Disabled

                                                                                                                                                          Disabled = 0
                                                                                                                                                          • Prints no logs.

                                                                                                                                                          member Errors

                                                                                                                                                          Errors = 1
                                                                                                                                                          • Prints only errors.

                                                                                                                                                          member Warnings

                                                                                                                                                          Warnings = 2
                                                                                                                                                          • Prints errors and warnings.

                                                                                                                                                          enum PeerErrorType

                                                                                                                                                          enum PeerErrorType {
                                                                                                                                                          BrowserIncompatible = 'browser-incompatible',
                                                                                                                                                          Disconnected = 'disconnected',
                                                                                                                                                          InvalidID = 'invalid-id',
                                                                                                                                                          InvalidKey = 'invalid-key',
                                                                                                                                                          Network = 'network',
                                                                                                                                                          PeerUnavailable = 'peer-unavailable',
                                                                                                                                                          SslUnavailable = 'ssl-unavailable',
                                                                                                                                                          ServerError = 'server-error',
                                                                                                                                                          SocketError = 'socket-error',
                                                                                                                                                          SocketClosed = 'socket-closed',
                                                                                                                                                          UnavailableID = 'unavailable-id',
                                                                                                                                                          WebRTC = 'webrtc',
                                                                                                                                                          }

                                                                                                                                                            member BrowserIncompatible

                                                                                                                                                            BrowserIncompatible = 'browser-incompatible'
                                                                                                                                                            • The client's browser does not support some or all WebRTC features that you are trying to use.

                                                                                                                                                            member Disconnected

                                                                                                                                                            Disconnected = 'disconnected'
                                                                                                                                                            • You've already disconnected this peer from the server and can no longer make any new connections on it.

                                                                                                                                                            member InvalidID

                                                                                                                                                            InvalidID = 'invalid-id'
                                                                                                                                                            • The ID passed into the Peer constructor contains illegal characters.

                                                                                                                                                            member InvalidKey

                                                                                                                                                            InvalidKey = 'invalid-key'
                                                                                                                                                            • The API key passed into the Peer constructor contains illegal characters or is not in the system (cloud server only).

                                                                                                                                                            member Network

                                                                                                                                                            Network = 'network'
                                                                                                                                                            • Lost or cannot establish a connection to the signalling server.

                                                                                                                                                            member PeerUnavailable

                                                                                                                                                            PeerUnavailable = 'peer-unavailable'
                                                                                                                                                            • The peer you're trying to connect to does not exist.

                                                                                                                                                            member ServerError

                                                                                                                                                            ServerError = 'server-error'
                                                                                                                                                            • Unable to reach the server.

                                                                                                                                                            member SocketClosed

                                                                                                                                                            SocketClosed = 'socket-closed'
                                                                                                                                                            • The underlying socket closed unexpectedly.

                                                                                                                                                            member SocketError

                                                                                                                                                            SocketError = 'socket-error'
                                                                                                                                                            • An error from the underlying socket.

                                                                                                                                                            member SslUnavailable

                                                                                                                                                            SslUnavailable = 'ssl-unavailable'
                                                                                                                                                            • PeerJS is being used securely, but the cloud server does not support SSL. Use a custom PeerServer.

                                                                                                                                                            member UnavailableID

                                                                                                                                                            UnavailableID = 'unavailable-id'
                                                                                                                                                            • The ID passed into the Peer constructor is already taken.

                                                                                                                                                              :::caution This error is not fatal if your peer has open peer-to-peer connections. This can happen if you attempt to a peer that has been disconnected from the server, but its old ID has now been taken. :::

                                                                                                                                                            member WebRTC

                                                                                                                                                            WebRTC = 'webrtc'
                                                                                                                                                            • Native WebRTC errors.

                                                                                                                                                            enum SerializationType

                                                                                                                                                            enum SerializationType {
                                                                                                                                                            Binary = 'binary',
                                                                                                                                                            BinaryUTF8 = 'binary-utf8',
                                                                                                                                                            JSON = 'json',
                                                                                                                                                            None = 'raw',
                                                                                                                                                            }

                                                                                                                                                              member Binary

                                                                                                                                                              Binary = 'binary'

                                                                                                                                                                member BinaryUTF8

                                                                                                                                                                BinaryUTF8 = 'binary-utf8'

                                                                                                                                                                  member JSON

                                                                                                                                                                  JSON = 'json'

                                                                                                                                                                    member None

                                                                                                                                                                    None = 'raw'

                                                                                                                                                                      enum ServerMessageType

                                                                                                                                                                      enum ServerMessageType {
                                                                                                                                                                      Heartbeat = 'HEARTBEAT',
                                                                                                                                                                      Candidate = 'CANDIDATE',
                                                                                                                                                                      Offer = 'OFFER',
                                                                                                                                                                      Answer = 'ANSWER',
                                                                                                                                                                      Open = 'OPEN',
                                                                                                                                                                      Error = 'ERROR',
                                                                                                                                                                      IdTaken = 'ID-TAKEN',
                                                                                                                                                                      InvalidKey = 'INVALID-KEY',
                                                                                                                                                                      Leave = 'LEAVE',
                                                                                                                                                                      Expire = 'EXPIRE',
                                                                                                                                                                      }

                                                                                                                                                                        member Answer

                                                                                                                                                                        Answer = 'ANSWER'

                                                                                                                                                                          member Candidate

                                                                                                                                                                          Candidate = 'CANDIDATE'

                                                                                                                                                                            member Error

                                                                                                                                                                            Error = 'ERROR'

                                                                                                                                                                              member Expire

                                                                                                                                                                              Expire = 'EXPIRE'

                                                                                                                                                                                member Heartbeat

                                                                                                                                                                                Heartbeat = 'HEARTBEAT'

                                                                                                                                                                                  member IdTaken

                                                                                                                                                                                  IdTaken = 'ID-TAKEN'

                                                                                                                                                                                    member InvalidKey

                                                                                                                                                                                    InvalidKey = 'INVALID-KEY'

                                                                                                                                                                                      member Leave

                                                                                                                                                                                      Leave = 'LEAVE'

                                                                                                                                                                                        member Offer

                                                                                                                                                                                        Offer = 'OFFER'

                                                                                                                                                                                          member Open

                                                                                                                                                                                          Open = 'OPEN'

                                                                                                                                                                                            enum SocketEventType

                                                                                                                                                                                            enum SocketEventType {
                                                                                                                                                                                            Message = 'message',
                                                                                                                                                                                            Disconnected = 'disconnected',
                                                                                                                                                                                            Error = 'error',
                                                                                                                                                                                            Close = 'close',
                                                                                                                                                                                            }

                                                                                                                                                                                              member Close

                                                                                                                                                                                              Close = 'close'

                                                                                                                                                                                                member Disconnected

                                                                                                                                                                                                Disconnected = 'disconnected'

                                                                                                                                                                                                  member Error

                                                                                                                                                                                                  Error = 'error'

                                                                                                                                                                                                    member Message

                                                                                                                                                                                                    Message = 'message'

                                                                                                                                                                                                      Package Files (1)

                                                                                                                                                                                                      Dependencies (4)

                                                                                                                                                                                                      Dev Dependencies (26)

                                                                                                                                                                                                      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>