LearnBoost/socket.io — Issue #723: Redis+Cluster clients count not in sync
Author: xpepermint
Created: 2012-01-18 23:08:00+00:00
State: open

I think this could be a bug. Example below prints the number of connected clients. If you run this code and connect to the server you will see that the number is +1. If you then disconnect from it only one worker will show 0 and others will still have +1. This should be in sync right? Thx.

########
test.coffee:
########

io = require('socket.io')

# My socket server
class SocketServer
constructor: (@port) ->
@sio = null
setInterval (=> # TEST
console.log "COUNT:#{@sio.sockets.clients().length}"), 1000

start: ->
@sio = io.listen @port
@sio.configure =>
@sio.disable 'browser client' # do not server assets
@sio.set 'store', new io.RedisStore
@sio.set 'transports', ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']
@sio.set 'log level', 3
@sio.configure 'production', =>
@sio.set 'log level', 1 # less verbose in production mode
@sio.sockets.on 'connection', (socket) => @connectHandler(socket)
console.log "listening on port #{@port} ..."
@

connectHandler: (socket) ->
console.log "CONNECTED"
socket.on 'disconnect', => @disconnectHandler()

disconnectHandler: ->
console.log "DISCONNECTED"

# Run cluster
cluster = require('cluster')
CPUs = require('os').cpus()
if (cluster.isMaster)
cluster.fork() for i in CPUs
diedHandler = (worker) ->
console.log('worker ' + worker.pid + ' died');
cluster.on('death', diedHandler)
else
server = new SocketServer(9000).start()