I really like the EventEmitter paradigm, and I'd like to use it to communicate between two programs across a network.
I came up with my own SockEmitter
, but I want to know: am I "doing it wrong(tm)"? Is there some package that will already do this? Is there a different paradigm that works better for communicating across a network?
Here's what I have:
var JsonSocket = require('json-socket')
// An event emitter that uses a JsonSocket.
// emit passes things over the wire, and data received
// over the wire calls the listeners.
//
// As a result, se.on('foo', console.log); se.emit('foo', 5)
// won't do what you might normally expect from an emitter.
function SockEmitter(socket) {
this._listeners = {}
this.sock = new JsonSocket(socket)
this.sock.on('message', this._message.bind(this))
}
SockEmitter.prototype = {
on: function (type, handler) {
if (!this._listeners[type]) {
this._listeners[type] = [];
}
this._listeners[type].push(handler)
},
off: function (type, handler) {
if (!this._listeners[type]) {
return false
}
var idx = this._listeners[type].indexOf(handler)
if (idx === -1) return false
this._listeners[type].splice(idx, 1)
},
emit: function (type) {
var args = [].slice.call(arguments, 1)
this.sock.sendMessage({type: type, args: args})
},
_message: function (message) {
if (!message || !message.type || !Array.isArray(message.args)) {
return console.error('Invalid message received: %s', message)
}
if (!this._listeners[message.type]) return
this._listeners[message.type].forEach(function (handler) {
handler.apply(null, message.args)
})
}
}
Socket.io is often brought up for something like this. It provides 2 way communication in an evented way. However, people often look over Server Sent Events which is a W3C standard for 1-way eventing that uses standard http messages (rather than doing an UPGRADE
to a custom protocol like socket.io does).
With SSE, you will probably see many articles focused on server-to-browser communication, but the the spec defines a simple http based message structure which can be used for whatever you want (eg: server-to-server).
I've been using it with great success. It is fantastic that I can build evented server-to-server things- and then easily connect with a browser too. As expected, IE isn't onboard yet but most browsers are.
There are many SSE npm modules. I have used eventsource-node
for the "listening" end with great success. When I got started with SSE, there were no modules for sending events- just modules for listening; so I wrote my own. It was no a big deal though- the protocol is so dead simple that it only took about 100 lines of code. I'm sure one of the published modules will work great.
The solution using json-socket would work if peer's IP address is always known - a typical client/server model where many TCP clients connect to a single TCP server. If you need to send events to all other clients, then you will need to implement a sort of chat like application on the server where the server forwards a message received from a client to the rest of the clients. Then, as shennan mentioned, the use of socket.io may be a solution for you. If you need direct N-to-N, or peer-to-peer connections, maintaining peer addresses (which could change overtime) would be a nightmare..
One other solution is to use Redis pub/sub feature. (this is one of popular solution for cluster communication in datacenters. There's a nice tool called 'devents'. https://npmjs.org/package/devents