spierce7
spierce7

Reputation: 15746

Sharing thousands of open websockets between nodejs forked processes

I'm working on a project that will consist of a mobile application, a desktop application, and a server. The server will need to maintain an open socket with the desktop application at all times (how does dropbox do it!?). When the mobile app is opened, a socket will be opened between the mobile app and the server, and the desktop application will send events to the mobile app (probably fairly low bandwidth), and vice versa.

The biggest hurdle for the server is keeping so many sockets open at once on a fairly modest server. Most of the time, the sockets will be idle as people won't be using the app. I'm only one person, and I don't want to pay a bunch of money in server costs, so I thought I'd choose nodejs with it's ability to hold large numbers of websockets open with low memory usage (normally I'd use Java, but I imagine that memory would become an issue quickly).

The issue that I can't wrap my mind around is dealing with nodejs and the cluster api. Since the cluster api spawns a separate nodejs process, how would I share the websocket connections between the forked processes? If I can't do that, I'd have to somehow guarantee that the mobile websocket was opened on the same node process that it's desktop client websocket was opened on. Not sure I can do that either.

What are my options?

Upvotes: 3

Views: 1499

Answers (1)

adeneo
adeneo

Reputation: 318162

Socket.io has some documentation on how to use it across clusters, you need to store the "references" to the available sockets in a storage that is available to all clusters, and then use those stored references to determine the IP etc. of each connected user

http://socket.io/docs/using-multiple-nodes/

If that seems a little complicated, there's modules available that create a "store" for the socket references and make them available across all cluster instances

http://socketcluster.io/#!/
https://github.com/elad/node-cluster-socket.io

There are several other modules available as well, that make it easy to use sockets across clusters, just make sure they use some sort of implentation of "sticky session", and not just a basic Redis store, as that apparently has some scaling issues.

Upvotes: 1

Related Questions