Upgraded from 1.3.7 and lost sockets.sockets array
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!
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];
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!
As far as I am concerned its resolved, so im closing it.