user2283026
user2283026

Reputation:

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?

Upvotes: 92

Views: 101682

Answers (8)

Dracula
Dracula

Reputation: 1

A simple and effective approach from the Socket.IO documentation is to join a room using the userId whenever a new connection is established. This eliminates the need to manually manage a mapping between socketId and userId. Here’s how it works:

You can use the user ID to make the link between Express and Socket.IO:

io.on("connection", (socket) => {
  const userId = socket.request.user.id; // Extract user ID from the request
  socket.join(`user:${userId}`); // Use user ID as a room
});

Which allows you to easily broadcast an event to all the connections of a given user:

io.to(`user:${userId}`).emit("foo", "bar");

Upvotes: 0

Coder Gautam YT
Coder Gautam YT

Reputation: 2157

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.

Upvotes: 4

user2204339
user2204339

Reputation: 21

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);
  });
});

Upvotes: 2

Giannis Kosmas
Giannis Kosmas

Reputation: 71

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

Upvotes: 6

Meth
Meth

Reputation: 61

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/

Upvotes: 6

Dhiral Pandya
Dhiral Pandya

Reputation: 10619

@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);  
}

Upvotes: 17

Oleg
Oleg

Reputation: 23287

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.

Upvotes: 95

Mustafa Dokumacı
Mustafa Dokumacı

Reputation: 3015

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

Upvotes: 216

Related Questions