Network discovery, messaging and events.
npm install disco
disco
utilizes eventcast
module to advertise themselves on the network. Advertisement messages may contain information that recipient would need to connect to sender through something other than UDP or anything else.
Messages can be of any (reasonable) size because eventcast
fragments messages over 1KB in size and reassembles them. Of course, it's best to keep them short and sweet.
The goal of disco
is to provide node discovery over the network and monitor discovered node's health. Use disco
as a part of your project.
Create an instance of disco
and start it:
var disco = Disco(9000) // port 9000
disco.on('discovery', function(msg){
// handle message
})
disco.start()
By default, msg
will contain sender's host, port and id.
disco
maintains a list of discovered nodes. Each node provides host, port, id, hostname and the last received message. Nodes also have phi accrual failure detector attached to them that can be used to monitor node's availability.
disco
creates a REPL that provides access to all instance methods and properties. REPL binds to a random port unless replPort
is passed to the constructor.
$ telnet localhost 20001
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
disco> disco.stop()
true
disco>
TODO
###start([callback])
Starts disco
instance and calls callback
, if provided.
###stop([callback])
Stops disco
instance and calls callback
, if provided.
###set(event, [message], [handler])
event
String | Object Ifevent
is a string - default interval will be used, otherwiseevent.name
andevent.interval
are required.message
* optional Message to pass with the event. Can be anything serializable.handler
Function optional Handler function for this event. If omitted, event will be emitted ondisco
instance.
Attach an event that will fire on interval with provided message
. When received, message
will be passed to handler
or emitted on disco
instance.
###unset(event)
event
String event name
Removes event
.
###discovery(message, handler)
message
* Discovery messagehandler
Function Discovery message handler. Called with event name ("discovery") and the message.
Overrides the default discovery message and handler.
###getNodes Returns an array of known nodes.
##Nodes
Nodes returned by Disco#getNodes
represent instances of disco
that this instance is aware of. Node object contains:
id
Node ID.host
Node hostport
Node portinfo
Last received discovery messagephi
A function that returns suspicion level for this node.