solidipes
v0.4.3
Published
a partially connected mesh network with messaging and discovery
Downloads
16
Maintainers
Readme
Solidipes
A partially connected mesh network with messaging and discovery.
Motive
There are many solutions for service discovery for Node. Solidipes provides node discovery and low latency message routing to serve as a foundation for distributed service discovery.
Test
Run any of the following:
$ mocha
$ npm test
$ make test
Note: remember to npm install
!
Install
$ npm install solidipes
API
Solidipes exports a Network constructor.
var Network = require('solidipes');
var id = require('crypto').randomBytes(16).toString('hex');
var net = new Network({
// auto-generated if not included
id: id,
// listen on port 3000, defaults to a random open port
port: 3000
});
Network([options])
Create a new network instance.
var net = new Network({
// defaults to an auto-generated 32-digit hex string, provide this if useful
id: 'a94312d853e585bc41c1ff990f120b1b',
// the interval on which to garbage collect the seen messages structure
cleanup: 10000, // default
// the host to bind the server to, finds an external address if not provided
host: 'localhost',
// the port to listen on, defaults to a random open port
port: 3000,
// number of neighbor nodes to keep in contact with
locals: 8 // default
});
info()
Returns the formatted transport connection info.
seed(info)
Seed the network with transport info, which can be retrieved back calling network.info()
.
// get the transport info
var info = network.info();
// broadcast the info to another process somehow
// seed the network with the remote transport info
net.seed(info);
size()
Gets the size of the network in number of nodes.
net.size(); // => 0
to(target, [options])
Specifies the recipient and options of an event emit. If target is null, designates a global broadcast.
// broadcast to everybody! (realistically just do .emit)
net.to().emit(...);
// unicast to the specified node
net.to('a94312d853e585bc41c1ff990f120b1b').emit(...);
// multicast to two nodes
net.to(['a94312d853e585bc41c1ff990f120b1b', '783513fc00eb8b0962afb8a0a83fb1b6']).emit(...);
// direct-unicast to the specified node (must be a neighbor node)
net.to('a94312d853e585bc41c1ff990f120b1b', {direct: true}).emit(..., function(err) {
// err would be "no routable nodes" if there is no direct connection
});
// the redundant option specifies how much the message should be replicated
// higher numbers mean the message has a high likelyhood of reaching its destination
// lower numbers mean fewer wasted packets
net.to('a94312d853e585bc41c1ff990f120b1b', {redundant: 16}).emit(...);
emit(event, [args...], [callback])
Broadcast an event to all nodes in the network.
net.emit('hello', {these: 'are'}, ['useful', 'arguments'], function(err) {
// handle errors
});
Events
add(node)
Emitted when a node is added to the network, either manually or through node discovery.
net.on('add', function(node) {
// please do not modify node!
node: {
id: 'a94312d853e585bc41c1ff990f120b1b',
transport: 'tcp://127.0.0.1:4035'
}
});
remove(node)
Emitted when a node is removed from the network.
net.on('remove', function(node) {
// please do not modify node!
node: {
id: 'a94312d853e585bc41c1ff990f120b1b',
transport: 'tcp://127.0.0.1:4036'
}
});
garbage(count)
Emitted as message records are cleaned up on the provided interval.
net.on('garbage', function(count) {
// count is the number of messages garbage-collected
});
error(err)
General error event, usually emitted during parse. Make sure you also add an error listener on the transport.
net.on('error', function(err) {
// emitted when an error occurred
// make sure to listen for this so the event loop isn't destroyed
});
unknown(packet)
Emitted when solidipes receives a valid but unknown packet.
net.on('unknown', function(packet) {
// emitted when a packet is correctly parsed but cannot be acted upon
});
Target#emit(event, [args...], [callback])
Emit an event to the represented Target (returned by Network#to
).
net.to().emit('new-service', {
// arbitrary data
type: 'converter',
info: {
host: 'localhost',
port: 7777
}
}, {priority: 17}, function(err) {
// this callback just gives an indication as to whether the message was sent
// not very good guarantees right now
});
Roadmap
Okay. Many flaws fixed, still no roadmap.
License
The MIT License (MIT)
Copyright © 2014 GlobeSherpa
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.