Creates TCP connections between nodes without configuration
Creates TCP connections between nodes without configuration.
npm install tcpnet
If the module dosn't work checkout the
node-mdns install instructions.
var tcpnet = require'tcpnet';// Create a service called my-cluster, for every TCP connection// pipe the output to the terminalvar service = tcpnet'my-cluster'socketpipeprocessstdout;;// For every one second send a message to all other services on the networkvar randomId = require'crypto'randomBytes6toString'hex';setIntervalconsole.log'write to ' + serviceconnectionslength + ' nodes';serviceconnectionsforEachsocketwrite'message from ' + randomId + '\n';;1000;// Pick a random port, and use all available addressesservicelisten0 '0.0.0.0';
Not startup a few node.js processes with this code, and you will see this:
$node demo.jswrite to 2 nodesmessage from a6c953a8c1f1message from 4eba8c4e76e9write to 2 nodesmessage from a6c953a8c1f1message from 4eba8c4e76e9
In more detail this is a module for creating a fully connected node cluster, that means that each node is aware of all other running nodes by a TCP connection. For the time being this is limited to a local network, that means you can't connect nodes across the internet, however connection nodes on a local network (switch or router) works perfectly. But thoughts are being made.
This function creates a new service instance.
settings argument is required and can be a
string or an
Starting with the
string case. This is the name of the service and is
the only thing there must be shared between all services (nodes).
Example: this code creates two services, they share the same name so they will automaticly be connected.
var serviceA = tcpnet'my-cluster';serviceAlisten;var serviceB = tcpnet'my-cluster';serviceBlisten;
settings argument can also be an
object, this provides some more
options, there are given by the following paramenters:
namerequired - exactly the same as the
uuidoptional - each service instance must have a unique ID, if two service instances share the same connection they can't connect and may not be able to reach all other servies, with diffrent IDs. By default the uuid is created by the gmid module. Note there are two requirements for the uuid. One, it must be a hexidecimal string (e.g.
BADA55). Two, they all ids must have a fixed length (e.g.
The next argument
connection is a optional function there if given, will
be added as a
connection event handler.
Example: this code will do the same as the one below:
var service = tcpnet'my-cluster'// do something with the TCP socket;servicelisten;
var service = tcpnet'my-cluster';serviceon'connection'// do something with the TCP socket;servicelisten;
The first argument
port is no big surprise, its the port that the
service will use to receive other connections. By default
which is just a random available
port assigned by the OS.
The second argument
address is the address you want to listen on, by
0.0.0.0 which is all available addresses (localhost
and public). Do also note that
address can be a domain (e.q.
will just be resoved to a IP address by a DNS lookup.
Example: 3 ways to listen on only the loopback interface.
servicelisten0 '127.0.0.1';servicelisten0 '::1';servicelisten0 'localhost';
NOTE: because there apear to be a bug in
MDNS a public only address
192.168.0.198) is not allowed, if you wan't the service to be public
you will have to use
0.0.0.0 for the time being.
The third argument
callback is just an
listening event handler.
servicelistenconsole.log'service is ready';;
This method will return an object containing two properties:
NOTE: do not use this before the
listening has emitted, if you do so it will
Example: show the port and addresses that the service is listening on:
servicelisten0 '0.0.0.0'console.log'service is ready and listening on:';var info = serviceaddress;infoaddressesforEachconsole.log'- ' + address + ':' + infoport;;;/* Output could be:* - 127.0.0.1:52740* - 192.168.0.198:52740* - ::1:52740* - fe80::5ab0:35ff:fe84:84b1:52740*/
This is a dynamic array containing all online sockets. This means that sockets
will be added once the
connection event fires and removed once socket
Example: multicast a single message from this service to all other online services.
serviceconnectionsforEachsocketwrite'hallo message to you from me\n';;
This method will close the service and call
socket.end() on all online
callback arguments will be assigned as an
close event handler.
serviceclose// everything related to the service is now closed, unless there are other// this running node will automaticly shutdown this process.;
This event is emitted once a new socket becomes online, to detect the when
is offline use the socket
Example: how to detect remote service shutdown
serviceon'connection'socketonce'close'console.log'the service is most likely dead now';;;
error occurres in the
server or between establing a
and the actuall
connection event the
error event will emit. Be aware that
error handler exist the error will be throwen.
Example: on one way to handle errors (but almost like just throwing)
serviceon'error'console.errorerrstack;// oh no, an error we better just close the serviceserviceclose// if anything else is running, then it won't be shutdown gracefullyprocessexit1;;;
Once the service is online this event will emit, note that calling
service.address() is quite useless if this event hasn't been emitted.
The service is now closed dude to a
The software is license under "MIT"
Copyright (c) 2012 Andreas Madsen
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.