Socket.io: io.sockets.socket(socketId) is gone?

Created on 13 Jun 2014  Â·  16Comments  Â·  Source: socketio/socket.io

In the 0.9.x, we can use io.sockets.socket(socketId), but in 1.0.x, it's gone.
How to find a socket by id in 1.0.x?

Most helpful comment

Thank you @rsdrsd , io.to(socket.id).emit() is what I want.
I think the official docs didn't introduce this io.to(socketId) method clearly.

All 16 comments

Missing that one also.

If I am right, you can get a reference like:

io.sockets.connected[socketid]

But this isn't the preferred method and only works in the current process. There are more ways to get a reference. Just look at the console.log(io).

Hi @rsdrsd , thank you for your concern. io.sockets.connected[socketid] works but not in official docs.
I've checked console.log(io) but find nothing helps. Will you please point out which method in io works?
Thank you very much!

I think there isn't a method for this. Just searched the code and couldn't find a method. I also really need something "official" for this, which works also through the redis adapter.

According to official document, socket.id seems not used by any API.
So what's the official suggested solution to deal with "private chat" ? Use a custom JavaScript object to preserve username and socket?

{
    username1: Socket1,
    username2: Socket2,
    ...
}

That is possible, but every socket has its own room with its socket id as room name. You could send messages to that room for a private chat.

In your case something like io.to(socket.id).emit() should work.

I need to get an instance to a socket to let that socket join a room, but this seems impossible, because there isn't a method to get a socket reference from a socket.id.

I only can get that refrerence from that connection, but I need to get a reference to that socket from a different connection, because that one confirms that it can join that room.

Thank you @rsdrsd , io.to(socket.id).emit() is what I want.
I think the official docs didn't introduce this io.to(socketId) method clearly.

io.to(socketId) returns default (/) namespace, not room with one socket. And when I do .emit() all sockets of default namespace receive emitted message. Checked in 1.0.6, 1.1.0, 1.2.1.

io.sockets.connected[socketid] - works, but what about redis-adapter?

shouldn't it be.
io.connected[socket.id]

? without ".sockets."

Hi, I am using version 1.4.4 and none of the above methods work for me. I want to get a reference to a socket using it's socket.id.
All the above methods mentioned here does not work.
What I really want to do is get a socket reference of one namespace from another namespace. A tutorial gives the following code snippet, but uses 0.9.x version.

var self = this;
//--code--//
this.namespace_A = io.of("ns_a");
this.namespace_B = io.of("bs_b");
//--code--//
this.namespace_B.on("connection", function(socket){
        //--code--//
        var ns_a_soc = self.namespace_A.sockets[socket.id];
        //--code--//
});

I need an alternative to this line var ns_a_soc = self.namespace_A.sockets[socket.id]; for version 1.4.4. Can anyone please help?

same problem here

Seems to work fine here.
Maybe you need to socket.join('ns_a"); first?

Hello! I am using the latest release (2.0) and can't get io.to(socket.id) to work. Any news on this?

@phpmydev I’d recommend opening a new issue for that — this one seems to be for a different problem.

I'm curious if io.to(socket.id).join(roomID) is working too because I can't found in the documentation

@StefansArya io.to(socket.id) is meant to be used with emit (io.to(socket.id).emit('event'), sending a message to that particular socket), it does not return a Socket object.

@darrachequesne Thanks for the explanation, I have learn so many things with nodejs REPL and found that I can do this for the alternative... I'll leave this here if someone have a same problem like me..
io.sockets.sockets[targetSocketID].join(roomID)

Was this page helpful?
0 / 5 - 0 ratings