Dynamically maintains a tree overlay topology from nodes connected by WebRTC as they join and leave. Each node accepts a maximum number of new connections after which the newer connection requests are delegated to children.
Requires a publicly accessible server for bootstrapping the connections, provided by webrtc-bootstrap.
// On the root node
var bootstrap = require('webrtc-bootstrap')('server hostname or ip:port')
var Node = require('webrtc-tree-overlay')
var root = new Node(bootstrap).becomeRoot('secret')
root.on('child-connect', function (channel) {
channel.send('ping')
channel.on('data', function (data) {
console.log(data)
})
})
// On a child node
var boostrap = ...
var Node = ...
var node = new Node(bootstrap).join()
node.on('parent-connect', function (channel) {
channel.on('data', function (data) {
console.log(data)
channel.send('pong')
})
})
node.on('child-connect', function (channel) {
channel.send('ping')
})
Server process:
DEBUG='webrtc-tree-overlay*,webrtc-bootstrap*' bin/server
Root process:
DEBUG='webrtc-tree-overlay*,webrtc-bootstrap*' bin/root --host localhost:5000
Node process(es):
DEBUG='webrtc-tree-overlay*,webrtc-bootstrap*' bin/node --host localhost:5000 --origin nodejs_node
Browser process(s):
open http://localhost:5000/#browser_node
bootstrap is a webrtc-bootstrap connected client.
opts has the following defaults:
{ peerOpts: {}, maxDegree: 10, requestTimeoutInMs: 30 * 1000 }
where
opts.peerOpts
are options passed to SimplePeeropts.maxDegree
is the maximum number of children a node will keepopts.requestTimeoutInMs
the upper bound on the time a candidate will be considered for joining. If the connection handshake has not been successfully before the end of the interval, the candidate is rejected.
The current dictionary of children channels.
The current number of children.
The maximum number of children and candidates that are kept. If a join request arrives while it will either be passed to one of the children, or kept until one a candidate has become a connected child.
The parent channel, null if the node has not joined yet or is the root.
Become root (through the bootstrap client), after which the node will automatically handle join requests.
Join an existing tree (through the bootstrap client).
Close the node and all associated channels.
- data received from of the direct neighbours, either parent or children;
- channel: on which it was received;
- isParent whether the channel is from our parent (
true
) or one of our children (false
).
When the node is ready to communicate with its parent through channel.
When the parent channel has closed.
When the parent channel has failed become of an error err.
When the node is ready to communicate with new child through channel.
When the child channel has closed.
When then child channel has failed because of err.
Abstracts the underlying WebRTC channel to multiplex the tree join protocol with application-level messages. Lightweight messages can be sent on the topology on those channels. However, for any heavy data traffic a dedicated SimplePeer connection should be established. The channel can be used for that purpose.
The constructor is called internally by a Node during the joining process and it is not available publicly.
Identifier of the channel, different from the Node.id on either side. From the point of view of a child, the id of its parent is always null. From the point of view of a parent, each child has a non-null unique id.
Closes the channel.
Return true if the channel is used to communicate with the Node's parent.
Sends data to the neighbour (parent or child) through the channel.
Fires when the channel has closed.
Fires when data has arrived.
Fires an error aborted the channel.
Used internally. Fires when a join request has arrived. An application should not need to bother with those.