Class: Peer

Peer(idopt, options)

Class that manages all p2p connections and rooms.

Constructor

new Peer(idopt, options)

Create new Peer instance and connect to the signaling server.
Parameters:
Name Type Attributes Description
id string <optional>
User's peerId.
options Object Optional arguments for the connection.
Properties
Name Type Attributes Default Description
key string ECLWebRTC API key.
debug number <optional>
0 Log level. NONE:0, ERROR:1, WARN:2, FULL:3.
config object <optional>
config.defaultConfig A RTCConfiguration dictionary for the RTCPeerConnection.
turn boolean <optional>
true Whether using ECLWebRTC's TURN or not.
credential object <optional>
The credential used to authenticate peer.
Properties
Name Type Attributes Description
timestamp number <optional>
Current UNIX timestamp.
ttl number <optional>
Time to live; The credential expires at timestamp + ttl.
authToken string <optional>
Credential token calculated with HMAC.

Members

connections :object

Object contains all connections.
Type:
  • object

id :string

The Peer ID specified by a user or randomly assigned Peer ID by the signaling server.
Type:
  • string

open :boolean

Whether the socket is connecting to the signalling server or not.
Type:
  • boolean

rooms :object

Object contains all rooms.
Type:
  • object

Methods

call(peerId, streamopt, optionsopt) → {MediaConnection}

Calls the designated Peer and creates new MediaConnection.
Parameters:
Name Type Attributes Description
peerId string The peerId of the peer you are calling.
stream MediaStream <optional>
The MediaStream to send to the remote peer. If not set, the caller creates offer SDP with `recvonly` attribute.
options object <optional>
Optional arguments for the connection.
Properties
Name Type Attributes Description
label string <optional>
Label to easily identify the connection on either peer.
videoBandwidth number <optional>
A max video bandwidth(kbps)
audioBandwidth number <optional>
A max audio bandwidth(kbps)
videoCodec string <optional>
A video codec like 'H264'
audioCodec string <optional>
A video codec like 'PCMU'
Returns:
An instance of MediaConnection.
Type
MediaConnection

connect(peerId, optionsopt) → {DataConnection}

Connects to the designated Peer and creates new DataConnection.
Parameters:
Name Type Attributes Description
peerId string User's peerId.
options Object <optional>
Optional arguments for DataConnection.
Properties
Name Type Attributes Description
label string <optional>
Label to easily identify the connection on either peer.
metadata string <optional>
Any additional information to send to the peer.
serialization string <optional>
How to serialize data when sending. One of 'binary', 'json' or 'none'.
Returns:
An instance of DataConnection.
Type
DataConnection

destroy()

Close all connections and disconnect socket.

disconnect()

Close socket and clean up some properties, then emit disconnect event.

joinRoom(roomName, roomOptionsopt) → {SFURoom|MeshRoom}

Join fullmesh type or SFU type room that two or more users can join.
Parameters:
Name Type Attributes Description
roomName string The name of the room user is joining to.
roomOptions object <optional>
Options to configure connection
Properties
Name Type Attributes Default Description
mode string <optional>
'mesh' One of 'sfu' or 'mesh'.
stream MediaStream <optional>
Media stream user wants to emit.
videoBandwidth number <optional>
A max video bandwidth(kbps)
audioBandwidth number <optional>
A max audio bandwidth(kbps)
videoCodec string <optional>
A video codec like 'H264'
audioCodec string <optional>
A video codec like 'PCMU'
Returns:
An instance of SFURoom or MeshRoom.
Type
SFURoom | MeshRoom

listAllPeers(cb)

Call Rest API and get the list of peerIds assciated with API key.
Parameters:
Name Type Description
cb function The callback function that is called after XHR.

updateCredential(newCredential)

Update server-side credential by sending a request in order to extend TTL.
Parameters:
Name Type Description
newCredential object The new credential generated by user.
Properties
Name Type Attributes Description
timestamp number <optional>
Current UNIX timestamp.
ttl number <optional>
Time to live; The credential expires at timestamp + ttl.
authToken string <optional>
Credential token calculated with HMAC.

Events

call

Received a call from peer.
Type:

close

Finished closing all connections to peers.

connection

Received a connection from peer.
Type:

disconnected

Disconnected from the signalling server.
Type:
  • string
Properties:
Name Type Description
id string Peer ID

error

Error occurred.
Type:
  • Error

open

Successfully connected to signaling server.
Type:
  • string
Properties:
Name Type Description
id string Peer ID