Home Reference Source
public class | source

Communication

Communication is a facade to send messages to peers in a network using unicast or broadcast channels.

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public
public

network: *

public

unicast: *

Private Members
private
private
private
private

Method Summary

Public Methods
public

onBroadcast(callback: MessageCallback): void

Listen on broadcasted messages

public

onOnceBroadcast(callback: MessageCallback): void

Listen to a broadcasted message, then remove the listener

public

onOnceUnicast(callback: MessageCallback): void

Listen to an incoming unicasted message, and then remove the listener

public

onStreamBroadcast(callback: MessageCallback): void

Listen on incoming unicasted streams

public

onStreamUnicast(callback: MessageCallback): void

Listen on incoming unicasted streams

public

onUnicast(callback: MessageCallback): void

Listen on incoming unicasted message

public

Remove all 'receive' broadcast callback

public

Remove all 'receive' unicast callback

public

sendBroadcast(message: Object, id: Object, isReady: Object): Object

Send a message to all peers using broadcast, (optionnal: specify uniq message id and the id to wait, see: broadcast.js)

public

sendMulticast(ids: string[], message: Object): Promise

public

sendUnicast(id: string, message: Object): Promise

Send a message to a specified peer

public

streamBroadcast(isReady: VersionVector): StreamRequest

Begin the streaming of a message to all peers (using broadcast)

public

Begin the streaming of a message to another peer (using unicast)

public

use(middleware: Object, priority: Number): void

Register a middleware, with an optional priority

Private Methods
private

_closeStream(id: string): void

Close an open stream

private

_handleStreamMessage(id: string, message: Object, callback: function): void

Handle an incoming stream message

Public Constructors

public constructor() source

Public Members

public broadcast: * source

public network: * source

public unicast: * source

Private Members

private _activeStreams: * source

private _broadcastStreams: * source

private _middlewares: * source

private _unicastStreams: * source

Public Methods

public onBroadcast(callback: MessageCallback): void source

Listen on broadcasted messages

Params:

NameTypeAttributeDescription
callback MessageCallback

Callback invoked with the message

Return:

void

public onOnceBroadcast(callback: MessageCallback): void source

Listen to a broadcasted message, then remove the listener

Params:

NameTypeAttributeDescription
callback MessageCallback

Callback invoked with the message

Return:

void

public onOnceUnicast(callback: MessageCallback): void source

Listen to an incoming unicasted message, and then remove the listener

Params:

NameTypeAttributeDescription
callback MessageCallback

Callback invoked with the message

Return:

void

public onStreamBroadcast(callback: MessageCallback): void source

Listen on incoming unicasted streams

Params:

NameTypeAttributeDescription
callback MessageCallback

Callback invoked with a StreamMessage as message

Return:

void

Example:

const comm = getSomeCommunication();

comm.onStreamBroadcast((id, stream) => {
 console.log('a peer with id = ', id, ' is streaming data to me');
 stream.on('data', data => console.log(data));
 stream.on('end', () => console.log('no more data available from the stream'));
});

public onStreamUnicast(callback: MessageCallback): void source

Listen on incoming unicasted streams

Params:

NameTypeAttributeDescription
callback MessageCallback

Callback invoked with a StreamMessage as message

Return:

void

Example:

const comm = getSomeCommunication();

comm.onStreamUnicast((id, stream) => {
 console.log('a peer with id = ', id, ' is streaming data to me');
 stream.on('data', data => console.log(data));
 stream.on('end', () => console.log('no more data available from the stream'));
});

public onUnicast(callback: MessageCallback): void source

Listen on incoming unicasted message

Params:

NameTypeAttributeDescription
callback MessageCallback

Callback invoked with the message

Return:

void

public removeAllBroacastCallback(): void source

Remove all 'receive' broadcast callback

Return:

void

public removeAllUnicastCallback(): void source

Remove all 'receive' unicast callback

Return:

void

public sendBroadcast(message: Object, id: Object, isReady: Object): Object source

Send a message to all peers using broadcast, (optionnal: specify uniq message id and the id to wait, see: broadcast.js)

Params:

NameTypeAttributeDescription
message Object

Message to broadcast over the network

id Object
  • optional

{_e: <stringId>, _c: <Integer>} this uniquely represents the id of the operation

isReady Object
  • optional

{_e: <stringId>, _c: <Integer>} this uniquely represents the id of the operation that we must wait before delivering the message

Return:

Object

id of the message sent

public sendMulticast(ids: string[], message: Object): Promise source

Params:

NameTypeAttributeDescription
ids string[]

Array of ids to the send message

message Object

Message to send

Return:

Promise

Promise fulfilled when all message are sent

TODO:

  • Complete tests of this function Send a message to multiple peers

public sendUnicast(id: string, message: Object): Promise source

Send a message to a specified peer

Params:

NameTypeAttributeDescription
id string

Id of the peer

message Object

Message to send

Return:

Promise

Promise fulfilled when the message is sent

public streamBroadcast(isReady: VersionVector): StreamRequest source

Begin the streaming of a message to all peers (using broadcast)

Params:

NameTypeAttributeDescription
isReady VersionVector
  • optional
  • default: undefined

Id of the message to wait before this message is received

Return:

StreamRequest

Stream used to transmit data to all peers

Example:

const comm = getSomeCommunication();

const stream = comm.sendBroadcast();
stream.write('Hello');
stream.write(' world!');
stream.end();

public streamUnicast(id: string): StreamRequest source

Begin the streaming of a message to another peer (using unicast)

Params:

NameTypeAttributeDescription
id string

Id of the peer

Return:

StreamRequest

Stream used to transmit data to another peer

Example:

const comm = getSomeCommunication();
const peerID = getSomePeerID();

const stream = comm.streamUnicast(peerID);
stream.write('Hello');
stream.write(' world!');
stream.end();

public use(middleware: Object, priority: Number): void source

Register a middleware, with an optional priority

Params:

NameTypeAttributeDescription
middleware Object

The middleware to register

middleware.in function

Function applied on middleware input

middleware.out function

Function applied on middleware output

priority Number
  • optional
  • default: 0

(optional) The middleware priority

Return:

void

Private Methods

private _closeStream(id: string): void source

Close an open stream

Params:

NameTypeAttributeDescription
id string

The ID of the stream to close

Return:

void

private _handleStreamMessage(id: string, message: Object, callback: function): void source

Handle an incoming stream message

Params:

NameTypeAttributeDescription
id string

The id of the peer who sent the message

message Object

The stream message to process

callback function

The callback associated with the stream message

Return:

void