3

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

9
  • I haven't tried your code, but where is the io.listen(app) call? Commented May 11, 2012 at 2:35
  • I call it in my global vars: io = require('socket.io').listen(app) Commented May 11, 2012 at 2:38
  • 1
    I'm surprised that your example works at all -- it seems that you point your RedisStore at localhost:8888, which is also where your web server is listening. You should point your RedisStore at your redis server (default port 6379). Commented May 11, 2012 at 7:51
  • I believe socket.io creates the redis server itself, it's not something setup and already running on the machine. You're probably right it should be a different port and I've since set it to 6379, although that hasn't solved the problem. Commented May 11, 2012 at 11:23
  • 1
    No, that's definitely not the case. You need to run a redis server yourself in order to use the RedisStore. It seems weird to me that socket.io doesn't give you an error about this. Commented May 11, 2012 at 11:28

1 Answer 1

3

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.

[email protected] [email protected]

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.