0

I am using socket.io 2.0.1. Now, I would like to know how to send a private message to a particular client. like this way

io.sockets.connected[socketid].emit();

but in 1.0.x, it's gone. I think there isn't a method for this. Just searched the code and couldn't find a method. I want to know how to send a message through a socket.id.

Rubel hasan
  • 2,522
  • 1
  • 13
  • 22
  • Possible duplicate of [Send message to specific client with socket.io and node.js](https://stackoverflow.com/questions/4647348/send-message-to-specific-client-with-socket-io-and-node-js) – node_modules Jul 12 '17 at 06:29
  • @C0dekid `socket.to(socketID).emit()` really is better :) – robertklep Jul 12 '17 at 06:29
  • this.socket.client.sockets(socketID).emit(). Its works for me. – Rubel hasan Jul 12 '17 at 06:34
  • @robertklep That's what I've commented, but removed because I thought it was wrong :) – node_modules Jul 12 '17 at 06:40
  • @robertklep Thanks for your comment. Is this right way to send a notification to the client through socket.id? – Rubel hasan Jul 12 '17 at 06:57
  • @Rubelhasan `io.to(socketid).emit(...)` is documented here: https://socket.io/docs/rooms-and-namespaces/#joining-and-leaving – robertklep Jul 12 '17 at 07:02
  • @robertklep actually I am implementing a notification system using reids pus/sub. everything is working fine. while I push a notification through socekt.id I am getting that issue. This way I can send a notification `this.socket.client.sockets[id].emit(name,message)`; – Rubel hasan Jul 12 '17 at 07:07
  • Thanks for you reply @robertklep. Do you suggest me any other way instead of this`this.socket.client.sockets[id].emit(name,message)`? – Rubel hasan Jul 12 '17 at 07:16
  • @robertklep it's is not working. this `this.socket.client.sockets[id].emit(name,message)` method just push last connected client. – Rubel hasan Jul 12 '17 at 09:39

0 Answers0