a duplex server with mesh network edge deduplication
A fancy tcp client and server that acts as neither, and abstracts out connection deduplication between identical nodes.
$ npm install masterless
Run any of the following:
$ mocha$ npm test$ make test
Note: remember to
npm install to get those yummy dev dependencies!
var Server = require'masterless';var left = 'e4663c9c3f9a89112afc48389a951e09';var right = 'b073a8ee09e1daca57e9d54a5efe5684';lefton'listening'rightconnectinfo;;righton'connect'rightsendid type: 'counter' total: 0;;lefton'message' onmessage;righton'message' onmessage;if packettype === 'counter'packettotal % 100 === 0 && console.log'counter at' packettotal;thissendsender type: 'counter' total: packettotal + 1;elseconsole.log'unknown packet' packet;
For more examples, see examples.
Construct a new server object, with an id unique to context in which the server is used. Optionally specify a
port to listen on.
var server = '67d2cb3db178bc5ca7d9f7157e3119aa';var serverWithPort = '81358297b83f2d4d6cfa58ba5aff6180' port: 23412;
Connect to a remote node via a transport uri. Automatically sets the node as a keep node.
Disconnect from a remote via a node id.
Reconnects to the specified node if the connection dies.
Returns a formatted transport uri. The result of this function is included in the
listening event. The
info method returns
null if the server has yet to initialize.
server.info; // => 'tcp://192.168.1.64:35649'
Ends all connections and closes the server.
serverclose// server has been closed;
Sends a packet to one or more nodes. Returns false if unable to send for a single destination.
serversend'81358297b83f2d4d6cfa58ba5aff6180' some: 'data';serversendleftid rightid some: 'other' random: 'data';serversend'not-a-valid-node' not: 'sent'; // returns false
The following example will open two connections, then close exactly one as the handshake completes.
var left = ;var right = ;leftconnectright.info;rightconnectleft.info;
Send messages to multiple nodes with low overhead (doesn't reencode the packet).
serversendleftid rightid multiple: 'dispatch';
keep method to specify a connection is locally important, and the server will attempt to reconnect once between disconnecting.
var left = ;var right = ;// both nodes will unilaterally try to reconnectrightkeepleftid;leftconnectright.info;leftonce'connect'// causes a reconnectrightdisconnectleftid;;
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.