Socket.IO and Node.js Core Cluster

express, node.js, redis, socket.io, websocket

Solution

It seems the issue may be related to the node module that Socket.IO comes installed with.

When I installed redis (`npm install hiredis redis`) and created the clients for the RedisStore using the redis module, everything suddenly worked perfectly. I've been running for over an hour with ~500 concurrent connections and haven't seen a single error, and every node process is being utilized.

hiredis@0.1.14 redis@0.7.2

Running Redis 2.6rc3 on port 6379.

Update: In Node.s 0.8, it looks like the cluster library should be considerably more mature, so the above code/issues will probably become obsolete: http://nodejs.org/docs/v0.7.8/api/cluster.html

Problem

Is it possible to use Socket.IO with Node's core cluster (not the outdated module)? I can fork multiple workers and it seems to work fine; however, when opening a connection I get the error: `solve: warn - client not handshaken client should reconnect` Here's the relevant code snippet (with a few simple things like expressjs config removed): ``` if ( cluster.isMaster ) { for ( var i = 0; i < numCPUs; i++ ) { cluster.fork(); } } else { app.get('/', function (req, res) { res.sendfile( __dirname + '/public/html/index.html' ); }); io.configure( function() { var RedisStore = require('socket.io').RedisStore, opts = { host: 'localhost', port: 8888 }; io.set('store', new RedisStore( { redisPub: opts, redisSub: opts, redisClient: opts } )); }); app.listen( 8888 ); io.sockets.on('connection', function (socket) { socket.emit( 'some', 'data' ); }); } ``` I've tried with and without using RedisStore and with the trick on this site (which I believe is obsolete now): http://www.danielbaulig.de/socket-ioexpress/ I've also looked at the code at http://www.ranu.com.ar/2011/11/redisstore-and-rooms-with-socketio.html, although I don't see how that code is any different than using MemoryStore. All of my test connections are using Websockets (RFC 6455). This works fine if I set numCPUs to equal 1. Node.js version 0.6.17 Socket.io version 0.9.5 Expressjs version 2.5.9 Update - include console output (note, on this attempt the connection did ultimately work, although it threw the same errors): ``` info - socket.io started info - socket.io started info - socket.io started info - socket.io started info - socket.io started debug - served static content /socket.io.js debug - client authorized info - handshake authorized 17644195072103946664 debug - setting request GET /socket.io/1/websocket/17644195072103946664 debug - set heartbeat interval for client 17644195072103946664 debug - websocket writing 7:::1+0 warn - client not handshaken client should reconnect info - transport end (error) debug - set close timeout for client 17644195072103946664 debug - cleared close timeout for client 17644195072103946664 debug - cleared heartbeat interval for client 17644195072103946664 debug - discarding transport debug - client authorized info - handshake authorized 16098526291524652257 debug - setting request GET /socket.io/1/websocket/16098526291524652257 debug - set heartbeat interval for client 16098526291524652257 debug - websocket writing 7:::1+0 warn - client not handshaken client should reconnect info - transport end (error) debug - set close timeout for client 16098526291524652257 debug - cleared close timeout for client 16098526291524652257 debug - cleared heartbeat interval for client 16098526291524652257 debug - discarding transport debug - client authorized info - handshake authorized 13419993801561067603 debug - setting request GET /socket.io/1/websocket/13419993801561067603 debug - set heartbeat interval for client 13419993801561067603 debug - client authorized for debug - websocket writing 1:: debug - websocket writing 5:::{"some":"data","args":[11354]} debug - websocket writing 5:::{"some":"data","args":[36448]} ``` This is how the console output ends on a failure (fails about 9 times out of 10): ``` info - transport end by forced client disconnection debug - websocket writing 0:: info - transport end (booted) debug - set close timeout for client 1639301251431944437 debug - cleared close timeout for client 1639301251431944437 debug - cleared heartbeat interval for client 1639301251431944437 debug - discarding transport debug - got disconnection packet debug - got disconnection packet ``` Update - Added links to possible tickets on github: https://github.com/LearnBoost/socket.io/issues/881 https://github.com/LearnBoost/socket.io/issues/438

Original source