Room leaving not synced with the cluster on disconnect.
Disconnects cause a socket to leave all rooms, but these leaves are not broadcast, so other workers never remove a disconnected socket from their rooms array.
Simple cluster test:
cluster = require 'cluster'
if cluster.isMaster
cluster.fork() for i in [1..2]
# Restart dead workers.
cluster.on 'death', (worker) ->
console.log "worker #{worker.pid} died"
cluster.fork()
else
resp = """
<script src="/socket.io/socket.io.js"></script>
<script> var socket = new io.connect(); </script>
<h1>Hello World.</h1>
"""
redis = require 'redis'
sio = require 'socket.io'
express = require 'express'
app = express.createServer()
app.get '/', (req, res) -> res.send resp
app.listen(3334)
io = sio.listen(app)
io.configure ->
store = new sio.RedisStore
io.set 'store', store
io.set 'log level', 1
io.sockets.on 'connection', (socket) ->
console.log "#{process.pid} client connect #{socket.id}"
socket.join 'secret_room'
io.sockets.in('secret_room').emit 'join', 'client'
socket.on 'disconnect', ->
console.log "#{process.pid} did disconnect of #{socket.id}"
setInterval ->
console.log "#{process.pid}", io.sockets.manager.rooms['/secret_room']
, 10000
Sample from opening in browser, and refreshing once:
42058 heard connect of 774353954845812834
42057 client connect 774353954845812834
42057 [ '774353954845812834' ]
42058 [ '774353954845812834' ]
42057 did disconnect of 774353954845812834
42057 heard connect of 18990979991629805474
42058 client connect 18990979991629805474
42057 [ '18990979991629805474' ]
42058 [ '774353954845812834', '18990979991629805474' ]
There are two fixes I've tried that work, but I'm unsure as to which is the best solution:
Inside the room leaving loop of Manager.prototype.onDisconnect:
this.store.publish('leave', id, room);
Or inside Manager.prototype.onClientDisconnect:
this.store.publish('disconnect', id);
I notice that most of the client disconnection sync is done via 'disconnect:' + id, which is the reason why the disconnections aren't picked up by other workers. Is there a strong reason not to use globally published disconnect events?
I am experiencing this issue as well
looks like this also happens for custom events too, only gets pushed to one instance
Confirm this. Using
this.store.publish('leave', id, room);
solved it for me.
got this issue with current what's available on npm. disconnects would leave the default room "" populated.
"Or inside Manager.prototype.onClientDisconnect: this.store.publish('disconnect', id);" seems to fix the issue.
Why is this still not official?
I face the same problem, any solution?