Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO socket.id changing for same client without reconnect

I'm making an application where I need to map sockets to some object. I thought I would be safe using the socket.id variable provided by Socket.IO. Unfortunately while debugging I found out that the socket.id changed without the client disconnecting/reconnecting or whatever.

Here's a small piece of code to show what I'm trying to do:

var io         = require('socket.io').listen(8080),
var myVariable = new Array();

// Main event loop
io.sockets.on('connection', function (socket) {
    socket.on('myEvent', function(data) {
        myVariable[socket.id] = data;
    }
    // 'someOtherEvent' which uses the mapped data.
    socket.on('someOtherEvent', function(data) {
        // Doesn't always work because socket.id has changed and var is empty
        doSomethingWith(myVariable[socket.id]);
    }
});

I'm not sure but I don't think this is the desired effect. Why does this happen and how would I work around it?

like image 277
Ruben Homs Avatar asked Dec 05 '25 10:12

Ruben Homs


1 Answers

I was using node-inspector to debug my application and because I was holding the code on some breakpoint it disconnected the client (probably some timeout client side). That's why I got a new socket.id when I continued the code execution.

like image 66
Ruben Homs Avatar answered Dec 08 '25 00:12

Ruben Homs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!