Home Reference Source
import N2N from 'n2n-overlay-wrtc'
public class | source

N2N

Extends:

events~EventEmitter → N2N

A peer has an inview and an outview, i.e., tables containing sockets to communicate with remote peers. This module transforms a peer so it can act as a bridge between its direct neighbors. Consequently, these neighbors can create their own communication channels: necessary data to establish the connection travel through the bridge; once the connection is successfully established, they communicate using their own direct connection.

Constructor Summary

Public Constructor
public

constructor(options: object)

Member Summary

Public Members
public

II: *

public

IO: *

public

NI: *

public

NO: *

public

PEER: *

public

PID: *

public

i: *

public

o: *

public

options: *

Method Summary

Public Methods
public

connect(from: function | MResponse | string | null, to: MRequest | string | null)

Create an arc (establishes a WebRTC connection if need be) from 'from' to 'to'.

public

disconnect(peerId: string)

Remove an arc of the outview or all arcs

public

Getter of the inview.

public

Getter of the inview ID.

public

Getter of the outview.

public

Getter of the inview ID.

public

neighbours(): [Object]

Return living neighbours as specified in neighborhood-wrtc

public

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

Send a message using either the inview or the outview.

public

stream(peerId: string, media: MediaStream, retry: number): promise

Send a MediaStream using either the inview or the outview.

public

uniqNeighbours(transform: Boolean): [type]

Return an array of uniq reachable peers without distinction between inview or outview (without -I or -O) if you want to send a message to one of these peers, add either a -I or a -I (or pass a boolean as parameter, default false)

Private Methods
private
private

_connected(peerId: string, isOutgoing: boolean)

private

_direct(peerId: string, message: string)

private
private

_failed(peerId: string, isOutgoing: boolean)

private
private

_received(peerId: string, message: object)

private

_streamed(peerId: string, stream: object)

Public Constructors

public constructor(options: object) source

Params:

NameTypeAttributeDescription
options object
  • optional

options represented as an object (refer to neighborhood-wrtc for other options).

options.pid string
  • optional

The unique identifier of the protocol.

options.retry number
  • optional
  • default: 5

The number of times it tries to send a message.

options.inview Neighborhood
  • optional

The neighborhood used for inviews, i.e., incoming arcs.

options.outview Neighborhood
  • optional

The neigbhorhood used for outviews, i.e., outgoing arcs.

Public Members

public II: * source

public IO: * source

public NI: * source

public NO: * source

public PEER: * source

public PID: * source

public i: * source

public o: * source

public options: * source

Public Methods

public connect(from: function | MResponse | string | null, to: MRequest | string | null) source

Create an arc (establishes a WebRTC connection if need be) from 'from' to 'to'. (TODO) explain function args

Params:

NameTypeAttributeDescription
from function | MResponse | string | null

The identifier of the peer that must initiate the connection. Null implicitely means this.

to MRequest | string | null

The identifier of the peer that must accept the connection. Null implicitely means this.

public disconnect(peerId: string) source

Remove an arc of the outview or all arcs

Params:

NameTypeAttributeDescription
peerId string

The identifier of the arc to remove.

public getInview(): Map source

Getter of the inview.

Return:

Map

A new map comprising {peerId => occurrences}.

public getInviewId(): string source

Getter of the inview ID.

Return:

string

The identifier of the inview.

public getOutview(): Map source

Getter of the outview.

Return:

Map

A new map comprising {peerId => occurrences}.

public getOutviewId(): string source

Getter of the inview ID.

Return:

string

The identifier of the outview.

public neighbours(): [Object] source

Return living neighbours as specified in neighborhood-wrtc

Return:

[Object]

Object containing living inview and living outview entries

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

Send a message using either the inview or the outview.

Params:

NameTypeAttributeDescription
peerId string

The identifier of the receiver.

message object

The message to send.

retry number
  • optional
  • default: 0

Number of times it retries to send a message.

Return:

promise

Promise that resolves if the message is sent, reject otherwise.

public stream(peerId: string, media: MediaStream, retry: number): promise source

Send a MediaStream using either the inview or the outview.

Params:

NameTypeAttributeDescription
peerId string

The identifier of the receiver.

media MediaStream

The message to send.

retry number
  • optional
  • default: 0

Number of times it retries to send a message.

Return:

promise

Promise that resolves if the message is sent, reject otherwise.

public uniqNeighbours(transform: Boolean): [type] source

Return an array of uniq reachable peers without distinction between inview or outview (without -I or -O) if you want to send a message to one of these peers, add either a -I or a -I (or pass a boolean as parameter, default false)

Params:

NameTypeAttributeDescription
transform Boolean
  • optional
  • default: false

If true, transform final Id into ids that can be used to send messages

Return:

[type]

[description]

Private Methods

private _bridge(peerId: string, msg: MConnectTo | MForwardTo | MForwarded) source

Params:

NameTypeAttributeDescription
peerId string

The identifier of the peer that sent us the message

msg MConnectTo | MForwardTo | MForwarded

The message received.

private _connected(peerId: string, isOutgoing: boolean) source

Params:

NameTypeAttributeDescription
peerId string

The identifier of the peer reachable through the newly added arc.

isOutgoing boolean

State if the added arc is outgoing or not.

private _direct(peerId: string, message: string) source

Params:

NameTypeAttributeDescription
peerId string

The identifier of the peer that we received a message from.

message string

The received message.

private _disconnected(peerId: string) source

Params:

NameTypeAttributeDescription
peerId string

The identifier of the peer that removed an arc.

private _failed(peerId: string, isOutgoing: boolean) source

Params:

NameTypeAttributeDescription
peerId string

The identifier of the peer we failed to establish a connection with.

isOutgoing boolean

State whether or not the failed arc was supposed to be an outgoing arc.

private _pid(): string source

Return:

string

The identifier of this protocol.

private _received(peerId: string, message: object) source

Params:

NameTypeAttributeDescription
peerId string

The identifier of the peer that we received a message from.

message object

The message received.

private _streamed(peerId: string, stream: object) source

Params:

NameTypeAttributeDescription
peerId string

The identifier of the peer that we received a message from.

stream object

The stream received.