91

I want to sent data to one specific socket ID.

We used to be able to do this in the older versions:

io.sockets.socket(socketid).emit('message', 'for your eyes only');

How would I go about doing something similar in Socket.IO 1.0?

7 Answers7

213

In socket.io 1.0 they provide a better way for this. Each socket automatically joins a default room by self id. Check documents: http://socket.io/docs/rooms-and-namespaces/#default-room

So you can emit to a socket by id with following code:

io.to(socketid).emit('message', 'for your eyes only');
Mustafa Dokumacı
  • 2,960
  • 2
  • 15
  • 11
  • There is difference between to (room_id) and (user_socket_id)? I actually use it to send message to a room that can have one or more users. "This makes it easy to broadcast messages to other sockets:", then can be one or more, not a unique private one. – Washington Botelho Sep 22 '14 at 20:16
  • @WashingtonBotelho, socket IDs usually look something like "Uj5CRqZ5b_Xubx9sAAAA", so in theory there are might be more than one user in such room, but in practice I don't know any cases when I would add a user to other user's personal room. – Oleg Oct 08 '14 at 07:59
  • 11
    How can we identify a specific user by their socketId, does this mean we need to store a key value pair of user (for eg-username) and socketId somewhere? – Sai Aug 14 '15 at 15:38
  • @Mustafa Dokumacı merhaba, bana bir yardim et http://stackoverflow.com/questions/38817680/nodejs-could-not-detect-online-soket – mahdi pishguy Aug 07 '16 at 19:54
  • It's just so unlogic... why don't just provide us a way to send a message to a socket id number and that's it? Anyway upvoted, the only work around that actually works on 2.x – Adry Dec 04 '17 at 09:05
  • @Sai I know this is kind of old, but the array.prototype.find() method may help – eeze Apr 20 '18 at 17:36
95

In socket.io 1.0 you can do that with following code:

if (io.sockets.connected[socketid]) {
    io.sockets.connected[socketid].emit('message', 'for your eyes only');
}

Update:

@MustafaDokumacı's answer contains a better solution.

Oleg
  • 22,300
  • 9
  • 68
  • 84
  • 4
    The difference between this answer and pointed better answer is that with THIS solution, you can have callbacks which is nice. But with io.to(socketid), you CAN'T use callbacks. – ramazan polat Oct 27 '16 at 17:33
17

@Mustafa Dokumacı and @Curious already provided enough information, I am adding how you can get socket id.

To get socket id use socket.id:

var chat = io.of("/socket").on('connection',onSocketConnected);

function onSocketConnected(socket){
   console.log("connected :"+socket.id);  
}
Dhiral Pandya
  • 10,311
  • 4
  • 47
  • 47
6

If you have used a namespace I found that the following works :

//Defining the namespace <br>
var nsp = io.of('/my-namespace');

//targeting the message to socket id <br>
nsp.to(socket id of the intended recipient).emit('private message', 'hello');

More about namespaces: http://socket.io/docs/rooms-and-namespaces/

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
Meth
  • 61
  • 1
  • 3
6

I believe both @Curious and @MustafaDokumacı provided solutions that work well. The difference though is that with @MustafaDokumacı's solution the message is broadcasted to a room and not only to a particular client.

The difference is prominent when an acknowledgement is requested.

io.sockets.connected[socketid].emit('message', 'for your eyes only', function(data) {...});

works as expected, while

io.to(socketid).emit('message', 'for your eyes only', function(data) {...});

fails with

Error: Callbacks are not supported when broadcasting
2

in Node.js --> socket.io --> there is a chat example to download Paste this in line (io on connection) part.. i use this code that works 100%

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    console.log(socket.id);
    io.to(socket.id).emit('chat message', msg+' you ID is:'+socket.id);
  });
});
2

Update Socket.io v4.0+

var socketById = io.sockets.sockets.get(id);
socketById.emit("message", "hi")

This is the optimal way to get a socket by ID in v4 and emitting to it.

Coder Gautam YT
  • 1,044
  • 7
  • 20