socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Room leaving not synced with the cluster on disconnect.

Open c-spencer opened this issue 14 years ago • 6 comments

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?

c-spencer avatar Apr 13 '12 09:04 c-spencer

I am experiencing this issue as well

wavded avatar Apr 13 '12 17:04 wavded

looks like this also happens for custom events too, only gets pushed to one instance

wavded avatar Apr 13 '12 17:04 wavded

Confirm this. Using

this.store.publish('leave', id, room);

solved it for me.

jjoschyy avatar Apr 28 '12 11:04 jjoschyy

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.

lassikin avatar Aug 20 '12 10:08 lassikin

Why is this still not official?

FREEZX avatar Mar 12 '13 11:03 FREEZX

I face the same problem, any solution?

yuxizhou avatar May 09 '13 07:05 yuxizhou