Does anyone know a good solution for scaling a node.js application - socket.io on multiple cores? I am currently testing the solution presented in the socket.io documentation for using socket.io on multiple nodes, but without concrete success.
I created a playing field for this on github: https://github.com/liviuignat/socket.io-clusters , which is a slightly modified copy of the chat application from socket.io. It uses express , cluster , socket.io@1.1.0 and socket.io-redis .
Currently, there is also an implementation using sticky-session in the feature/sticky branch, which seems to work better.
In the end, the application should be published in Heroku , scaled to multiple speakers.
Initially, I tried to do something like this - to start the server only for cluster nodes, but I always get an error: failed: the connection is closed before receiving a handshake response
if (cluster.isMaster) { for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); } else { var server = new Server({ dirName: __dirname, enableSocket: true }) .setupApp() .setupRoutes() .start(); }
Then I tried to start the server also for the master nodes:
if (cluster.isMaster) { var server = new Server({ dirName: __dirname, enableSocket: true }) .setupApp() .setupRoutes() .start(); for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); } else { var server = new Server({ dirName: __dirname, enableSocket: true }) .setupApp() .setupRoutes() .start(); }
I also tried this using both sticky-session and socket.io-redis in the feature/sticky branch, which seems to work with success, but still is not a good solution:
if (cluster.isMaster) { sticky(function() { var server = new Server({ dirName: __dirname, enableSocket: true }) .setupApp() .setupRoutes(); return server.http; }).listen(3000, function() { console.log('server started on 3000 port'); }); for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); } else { sticky(function() { var server = new Server({ dirName: __dirname, enableSocket: true }) .setupApp() .setupRoutes(); return server.http; }).listen(3000, function() { console.log('server started on 3000 port'); }); }
I will be doing more tests in the coming days, but it would help a lot if someone could come up with some ideas.
Thanks,