Peer API Reference
Peer
const peer = new Peer([id], [options]);
A peer can connect to other peers and listen for connections.
[id] (string)
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.
It's not recommended that you use this ID to identify peers, as it's meant to be used for brokering connections only. You're recommended to set the metadata option to send other identifying information.
[options] (object)
key(string): API key for the cloud PeerServer. This is not used for servers other than0.peerjs.com.warningPeerServer cloud runs on port 443. Please ensure it is not blocked or consider running your own PeerServer instead.
host(string): Server host. Defaults to0.peerjs.com. Also accepts'/'to signify relative hostname.port(number): Server port. Defaults to443.pingInterval(number): Ping interval in ms. Defaults to5000.path(string): The path where your self-hosted PeerServer is running. Defaults to'/'.secure(boolean):trueif you're using SSL.tipNote that our cloud-hosted server and assets may not support SSL.
config(object): Configuration hash passed to RTCPeerConnection. This hash contains any custom ICE/TURN server configuration. Defaults to{ 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }], 'sdpSemantics': 'unified-plan' }debug(number): Prints log messages depending on the debug level passed in. Defaults to0.0: Prints no logs.1: Prints only errors.2: Prints errors and warnings.3: Prints all logs.
peer.connect
const dataConnection = peer.connect(id, [options]);
Connects to the remote peer specified by id and returns a data connection. Be sure to listen on the error event in case the connection fails.
id (string)
The brokering ID of the remote peer (their peer.id).
[options] (object)
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 withdataConnection.label.metadata: Metadata associated with the connection, passed in by whoever initiated the connection. Can be accessed withdataConnection.metadata. Can be any serializable type.serialization(string): Can bebinary(default),binary-utf8,json, ornone. Can be accessed withdataConnection.serialization.tipbinary-utf8will take a performance hit because of the way UTF8 strings are packed into binary format.reliable(boolean): Whether the underlying data channels should be reliable (e.g. for large file transfers) or not (e.g. for gaming or streaming). Defaults tofalse.warningSetting reliable to true will use a shim for incompatible browsers (Chrome 30 and below only) and thus may not offer full performance.
peer.call
const mediaConnection = peer.call(id, stream, [options]);
Calls the remote peer specified by id and returns a media connection. Be sure to listen on the error event in case the connection fails.
id (string)
The brokering ID of the remote peer (their peer.id).
stream (MediaStream)
The caller's media stream.
[options] (object)
metadata: Metadata associated with the connection, passed in by whoever initiated the connection. Can be accessed withmediaConnection.metadata. Can be any serializable type.sdpTransform(method): Function which runs before create offer to modify sdp offer message.
peer.on
peer.on(event, callback);
Set listeners for peer events.
'open'
peer.on('open', function(id) { ... });
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.
'connection'
peer.on('connection', function(dataConnection) { ... });
Emitted when a new data connection is established from a remote peer.
'call'
peer.on('call', function(mediaConnection) { ... });
Emitted when a remote peer attempts to call you. The emitted mediaConnection is not yet active; you must first answer the call (mediaConnection.answer([stream]);). Then, you can listen for the stream event.
'close'
peer.on('close', function() { ... });
Emitted when the peer is destroyed and can no longer accept or create any new connections. At this time, the peer's connections will all be closed.
To be extra certain that peers clean up correctly, we recommend calling peer.destroy() on a peer when it is no longer needed.
'disconnected'
peer.on('disconnected', function() { ... });
Emitted when the peer is disconnected from the signalling server, either manually or because the connection to the signalling server was lost. When a peer is disconnected, its existing connections will stay alive, but the peer cannot accept or create any new connections. You can reconnect to the server by calling peer.reconnect().
'error'
peer.on('error', function(err) { ... });
Errors on the peer are almost always fatal and will destroy the peer. Errors from the underlying socket and PeerConnections are forwarded here.
These come in the following err.type flavors:
'browser-incompatible'(fatal): The client's browser does not support some or all WebRTC features that you are trying to use.'disconnected': You've already disconnected this peer from the server and can no longer make any new connections on it.'invalid-id'(fatal): The ID passed into the Peer constructor contains illegal characters.'invalid-key'(fatal): The API key passed into the Peer constructor contains illegal characters or is not in the system (cloud server only).'network': Lost or cannot establish a connection to the signalling server.'peer-unavailable': The peer you're trying to connect to does not exist.'ssl-unavailable'(fatal): PeerJS is being used securely, but the cloud server does not support SSL. Use a custom PeerServer.'server-error'(fatal): Unable to reach the server.'socket-error'(fatal): An error from the underlying socket.'socket-closed'(fatal): The underlying socket closed unexpectedly.'unavailable-id'(sometimes fatal): The ID passed into the Peer constructor is already taken.warningThis error is not fatal if your peer has open peer-to-peer connections. This can happen if you attempt to reconnect a peer that has been disconnected from the server, but its old ID has now been taken.
'webrtc': Native WebRTC errors.
peer.disconnect
peer.disconnect();
Close the connection to the server, leaving all existing data and media connections intact. peer.disconnected will be set to true and the disconnected event will fire.
This cannot be undone; the respective peer object will no longer be able to create or receive any connections and its ID will be forfeited on the (cloud) server.
peer.reconnect
peer.reconnect();
Attempt to reconnect to the server with the peer's old ID. Only disconnected peers 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.
peer.destroy
peer.destroy();
Close the connection to the server and terminate all existing connections. peer.destroyed will be set to true.
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 (cloud) server, and all of its data and media connections will be closed.
peer.id (string)
The brokering ID of this peer. If no ID was specified in the constructor, this will be undefined until the open event is emitted.
peer.connections (object)
A hash of all connections associated with this peer, keyed by the remote peer's ID.
We recommend keeping track of connections yourself rather than relying on this hash.
peer.disconnected (boolean)
false if there is an active connection to the PeerServer.
peer.destroyed (boolean)
true if this peer and all of its connections can no longer be used.