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

Upgraded from 1.3.7 and lost sockets.sockets array

Open chewax opened this issue 10 years ago • 3 comments

Hi, i upgraded from 1.3.7 to 1.4.5 and the structure changed so my app crashed because of that. That is not the main concern though. The problem i am facing, and i have tried everything, is to assign a custom attribute on the socket object. This is what i am doing:

   io = socketio.listen(server);
      io.on('connection', function(client){
        client.on('handshake', function(userId, customer) {
            client.user_id = userId;
            client.join(customer);
            //client.user_id is correctly assigned here.
        });
    });

The issue is that i am not being able to find that property anymore. On 1.3.4 i would just io.sockets.sockets.filter(function(s){ return s.user_id == "myid" })

Is it possible that it does not get persisted and therefore lost? I really am in the dark here.

Any help would be appreciated. Thanks!

chewax avatar Oct 12 '16 14:10 chewax

Hi! The internals of socket.io has indeed changed between 1.3.7 and 1.4.5, the sockets object is now an hash instead of an array.

You should be able to go through the said hash to retrieve your user_id:

for (var socket in io.sockets.sockets) {
  if (socket.user_id == "myid") return socket;
}

Or, if your user_ids are unique, you could have another hash with user_id as key and socket.id as value:

var socketId = userHash["myid"];
var socket = io.sockets.sockets[socketId];

darrachequesne avatar Oct 12 '16 16:10 darrachequesne

Thanks @darrachequesne. I tried the former in all the possible shapes you could imagine but could not retrieve the user_id.

Nevertheless, I coded the latter as a workaround in the meantime, and kind of liked it better. Then i just need use

io.to(userHash["myId"]).emit()

I think it might be faster too so i am sticking to it. Thanks for the answer!

chewax avatar Oct 12 '16 20:10 chewax

As far as I am concerned its resolved, so im closing it.

chewax avatar Oct 12 '16 20:10 chewax