Events over multicast
Network event emitter.
npm install eventcast
I decided to write this because 1) multicast is interesting and 2) event emitters are awesome. So it's a learning exercise. And if you think something is amiss - please open an issue!
Eventcast uses UDP multicast to send BSON-serialized messages to multiple nodes and to receive them. The goal is to provide a network event emitter where nodes can dynamically exchange data via familiar event API.
Checkout example folder to see it in action.
eventcast uses optional
aes128 message encryption. Packets encrypted with user-provided passphrase. When encryption is enabled
eventcast adds a nonce to every packet (nonce is created using
Multicast does not typically support messages over ~1500 bytes as far as I know. Messages that exceed MTU size will be silently dropped.
eventcast uses message segmentation for messages larger than
maxPayloadSize (defaults to 1024). Message is reassembled on the receiving end.
When receiving segmented messages
eventcast will request retransmission of any lost segments (TODO: max number of retransmission requests).
Create an instance of
eventcast and add some events:
var ec1 = Eventcast9000 // port 9000ec1startec1on'myevent' handleEvent
var ec2 = Eventcast9000 // port 9000ec2startec2emit'myevent' 'hello'
eventcast 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 20001Trying 127.0.0.1...Connected to localhost.Escape character is '^]'.eventcast@hostname> eventcast.stop()trueeventcast>
eventcast constructor takes an option hash:
addressstring Interface to which UDP server binds. Defaults to
0.0.0.0. Best left unchanged unless you really know what you're doing.
portnumber UDP server port. Will be set at random which is not very useful. Port must be the same for instances that want to see each other.
multicastMembershipstring Multicast group to join. Defaults to
multicastInterfacestring Interface to use for multicast messages. Defaults to
multicastLoopbackboolean Receive multicast messages on the loopback interface. Must be set to
truefor instances on the same machine to see each other.
multicastTtlnumber Multicast scope (see this article). Defaults to 1 - messages will not leave the subnet and will not be forwarded by the router.
replPortnumber REPL port. Connect to this port to access built-in REPL. If not set explicitly - port is random (you can figure it out from logs).
replHoststring REPL interface. Defaults to
localhost, which is probably best.
encryptobject Encryption options if you want encryption. By default encryption is disabled.
encrypt.keystring Passphrase to use for encryption. No default.
maxPayloadSizenumber Threshold in bytes above which messages will be chunked. Defaults to 1024. Multicast messages are best kept under ~1500 bytes to avoid silent packet loss.
messageTtlnumber If chunked message is transmitted and no chunks received within this timeframe - message is discarded. Defaults to 3000 (3 seconds).
logboolean Enable/disable logging. Default to
logLevelstring Log level.
Eventcast is an instance of Event Emitter, therefore it supports Event Emitter API. Internally,
#emit is the only method that is not real EE method but a wrapper for
events.EventEmitter.emit that first sends out messages and then emits the event.
Sends out event as
event with supplied arguments. Arguments must be serializable into JSON because they're going to be sent over the network.
Adds a listener for
… and the rest of EventEmitter API.
(Eventcast uses bunyan to log) If called with no arguments - returns an array of levels for all streams.
level will set all streams to
level will set that component to given level. Eventcast has two components so far -
eventcast is the root logger.
Starts the UDP server.
callback will be called when server is bound and ready.
Stops the UDP server.
callback will be called when server is shutdown.