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

Is editing the socket ID in Socket.io not supported by the library?

Open viziionary opened this issue 10 years ago • 3 comments

This simplified issue evolved from a more complicated problem I asked about here. You can read this same question on Stack Overflow, here.

I read on Stack Overflow that I could change the unique ID associated with each client in Socket.io to a unique ID of my own format like this:

io.on("connection", function(socket) {
    socket.id = generateId(); //return unique ID of my own format
});

Well I was interested in doing this because I like to keep things simple, and I'm building a session based app where users share their session by sending a session code. I designed my app so that when a new session is created, it is named after the client's socket ID. This lets me better keep track of the session's identity in my debugging logs and it just keeps things simple in my opinion.

But I wanted the code to be shorter than a typical socket ID, and since I knew I could change the socket IDs, I decided to generate my own shorter simpler unique IDs.

But it looks like, after extensive testing and trying to figure out what's wrong with my code, that changing the socket IDs as shown in the simple code above, breaks the room functionality of Socket.io. So to elaborate on my code and prove this:

In Socket.io, each client automatically joins a room with its own ID as the name of that room so we can easily send a message that that specific client by using the to method. In my code, after I change the name, I have it join a new room with it's new ID as the name so this should continue to work that way, like this:

io.on("connection", function(socket) {
    socket.id = generateId();
    socket.join(socket.id);
});

But afterwards, sending a message to that room like this does not work as it should:

io.on("connection", function(socket) {
    socket.id = generateId();
    socket.join(socket.id);
    io.to(socket.id).emit('message', { message : 'Hello there!' }); 
});

on the client side, this message does not register for me. Is this a bug with Socket.io, or are socket IDs not intended to be changed in Socket.io without breaking the room functionality? Is editing the socket ID in Socket.io not supported by the library?

This question evolved from a more complicated related question I asked on SO and on Github.

viziionary avatar Apr 08 '16 02:04 viziionary

On Stack Overflow I was told that socket IDs cannot be edited in Socket.io without breaking internal mechanisms. So that answers this.

viziionary avatar Apr 08 '16 05:04 viziionary

It's been about a year after the question asked.

Nevertheless I write an answer on the stackoverflow.

efkan avatar Feb 27 '17 14:02 efkan

I have a question regarding this topic. I'm making a simple react-native chat app and I use express with socket.io for backend, with mongoDB as db. I require all my users to be logged in, and I check if they are logged in in socket middleware, allowing them to create connection and send messages only if they are. Since I'm using middleware, and since mongoDB already guarantees that their _ids will be unique for each user, and I already send them inside JWT to middleware, can I set this id as socket id in middleware, before it creates connection ? I already tried it, and it consoles the right _id as socket.id in on('connection'), but I don't know if it will make more problems later on, along the way.. ? Does anybody know the answer to this?

radanjovic avatar Jul 12 '22 11:07 radanjovic