Home Reference Source
import Neighborhood from 'neighborhood-wrtc/lib/neighborhood.js'
public class | source

Neighborhood

Extends:

events~Events → Neighborhood

Easy-to-use interface to establish multiple WebRTC connections using SimplePeer (npm: simple-peer)

Constructor Summary

Public Constructor
public

constructor(options: object)

Member Summary

Public Members
public

PEER: *

public

decode: *

public

dying: *

public

encode: *

public

living: *

public

options: {"socketClass": *, "peer": *, "config": *, "timeout": *, "pendingTimeout": *, "encoding": *, "decoding": *}

public

pending: *

Method Summary

Public Methods
public

connect(arg1: function | object, arg2: object): *

Create a WebRTC connection.

public

disconnect(peerId: string | undefined): *

Remove an arc.

public

neighbours(): [ELiving]

return an array of living sockets

public

send(peerId: string, message: object, retry: number): promise

Send a message to a remote peer.

public

stream(peerId: *, media: *, retry: number): *

Private Methods
private

_accept(sender: function, msg: MRequest)

private

_checkPendingEntry(entry: *)

private
private

_initiate(sender: function, jobId: String)

private
private

_sendRenegociateRequest(request: *, to: *, retry: number): *

private

_sendRenegociateResponse(response: *, to: *, retry: number): *

Public Constructors

public constructor(options: object) source

Params:

NameTypeAttributeDescription
options object
  • optional

the options available to the connections, e.g. timeout before

options.socketClass object
  • optional

simple-peer default socket class (usefull if you need to change the type of socket)

options.config object
  • optional

simple-peer options

options.timeout number
  • optional
  • default: 60000

Time to wait (in milliseconds) for dying socket

options.pendingTimeout number
  • optional
  • default: 10000

Time to wait (in milliseconds) for pending socket before neighborhood-wrtc assumes that a connection establishment failed, or before an unused connection is removed.

options.encoding function
  • optional

Method to customize message sent, default: return JSON.stringify(data);

options.decoding function
  • optional

Method to decode a received message, default: return JSON.parse(data);

Public Members

public PEER: * source

public decode: * source

public dying: * source

public encode: * source

public living: * source

public options: {"socketClass": *, "peer": *, "config": *, "timeout": *, "pendingTimeout": *, "encoding": *, "decoding": *} source

public pending: * source

Public Methods

public connect(arg1: function | object, arg2: object): * source

Create a WebRTC connection.

Params:

NameTypeAttributeDescription
arg1 function | object

Either a callback function to send the message to the remote peer (for instance, it can use a signaling server or the already created WebRTC connexions), or a message received from the remote peer.

arg2 object

The message received from a peer that initialized a WebRTC connection.

Return:

*

public disconnect(peerId: string | undefined): * source

Remove an arc. If it was the last arc, the WebRTC connexion is downgraded to the dying table. In this table, the connexion will be closed if none create it.

Params:

NameTypeAttributeDescription
peerId string | undefined

The identifier of the peer. If no arg, remove all arcs.

Return:

*

public neighbours(): [ELiving] source

return an array of living sockets

Return:

[ELiving]

a living entry with socket, peer id and number of occurences (arcs)

public send(peerId: string, message: object, retry: number): promise source

Send a message to a remote peer.

Params:

NameTypeAttributeDescription
peerId string

The remote peer to send the message to.

message object

The message to send.

retry number
  • optional
  • default: 0

Retry few times to send the message before giving up.

Return:

promise

Resolved when the message is sent, reject otherwise. Note that loss of messages is not handled by default.

public stream(peerId: *, media: *, retry: number): * source

Params:

NameTypeAttributeDescription
peerId *
media *
retry number
  • optional
  • default: 0

Return:

*

Private Methods

private _accept(sender: function, msg: MRequest) source

Params:

NameTypeAttributeDescription
sender function

The function that send the offer to the remote initiating peer.

msg MRequest

The request message containing offers, peerId, etc.

private _checkPendingEntry(entry: *) source

Params:

NameTypeAttributeDescription
entry *

private _finalize(msg: MResponse) source

Params:

NameTypeAttributeDescription
msg MResponse

The message containing an offer, a peerId etc.

private _initiate(sender: function, jobId: String) source

Params:

NameTypeAttributeDescription
sender function

A function called at each offer

jobId String

The jobId that initiate the connection

private _receiveInternalMessage(msg: *) source

Params:

NameTypeAttributeDescription
msg *

private _sendRenegociateRequest(request: *, to: *, retry: number): * source

Params:

NameTypeAttributeDescription
request *
to *
retry number
  • optional
  • default: 0

Return:

*

private _sendRenegociateResponse(response: *, to: *, retry: number): * source

Params:

NameTypeAttributeDescription
response *
to *
retry number
  • optional
  • default: 0

Return:

*