Search code examples
socket.iosocket.io-1.0

Sending message to a specific ID in Socket.IO 1.0


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?


Solution

  • 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');