In our application architecture we have multiple servers that handle different types of connections, namely:
- Requests from web clients (www and REST) over HTTPS
- Socket.io connections from web clients over HTTPS
- Socket.io connections from devices
- TCP connections from proxy servers for devices over TLS
Above servers are spawned as processes by a manager app, and this can be replicated on multiple virtual machines. The challenge was to allow all of these processes to talk to each other. One example is events coming from devices via proxy servers need to bubble up all the way to web client via Socket.io, which involves two servers – Socket.io with web client and TCP with proxy.
Solution was to adopt a dispatcher pattern using Redis as pub-sub intermediary and have API similar to Node.js
EventEmitter API, with additional methods
off() turns off callbacks for a specific event, and
flush() removes all listeners for given key. Redis server resides on a separate instance accessible to all servers in the cluster, therefore it can be shared by all subscribers and emitters.
Actual source code of this Dispatcher is below: