0

i'm working on a Node.js app in which we are using socket.io for sending and receiving events/messages to clients. Initially it is sending events to clients using the following statement.

io.sockets.emit('SequenceLoad', data);

But now i want to send events only to specific clients using client specific sockets. I have searched alot regarding this, found some ways but none of them seems to work, found on various SO posts.

Here is the code that i have tried:

const io = require('socket.io')(http);
var socketsCollection = [];
io.on('connection', function (socket) {

  //Saving socket id's for sending events to specific sockets based on their ids
  socketsCollection.push(socket.id);
  socket.on('SequenceLoadServer', function (data) {
     // logger.info('Socket Server | Event: sequenceLoadServer with socket server. \n Data:'+ data);
     console.log("In APP.js for SequenceLoadServer event");
     console.log("Socket Id :" + socket.id);
     var socketId = null;
     setTimeout(function () {
        for (var item of socketsCollection) {
           if(item == socket.id){
              socketId= item;
              break;   
           }
        }
        console.log("Socket after searching Id :" + socketId);
        io.sockets.emit('SequenceLoad', data);   -> This works at the moment

        //Tried the following ways as well but none of them worked 

        //io.sockets.connected[socketId].emit('SequenceLoad', data);
        //io.sockets.socket(socketId).emit('SequenceLoad', data); 
        //io.to(socketId).emit('SequenceLoad', data);
        //io.to('/#' + socketId).emit('SequenceLoad', data);
        //io.to(`${socketId}`).emit('SequenceLoad', data);

     }, 1000 * 3);

  });
}

Version of socket.io in my project:

"socket.io": "^2.2.0",
"socket.io-client": "^2.2.0"

Any help would great, correct me if i'm doing wrong because i'm new to it. Thanks

2
  • How do you want to select a particular socket/client? Via its id? Commented Jun 19, 2019 at 7:28
  • @NickParsons yes Commented Jun 19, 2019 at 7:54

1 Answer 1

1

Given you have the socket instance, you can send to a specific client directly by:

io.to(`${socket.id}`).emit(...);

See Emit cheatsheet

Sign up to request clarification or add additional context in comments.

4 Comments

You can see in my post that i'm already doing it but it is not working.
@Harry.Naeem that doesn't mean it's not the correct way to do this - also have you looked at my answer closely? I'm not using the ID you fetch from the array, I'm using the socket.id directly (this would be the same difference due to how closures work). I believe the issue is your loop / ID fetch isn't giving you the correct ID, try using the socket.id directly, I'd fully expect it to work.
I didn't said it is wrong way but i have tried this. My loop is giving me the correct id that i have saved and i'm using logs to see the socket id before emiting the data. But anyhow i will take a look into it as well
Yes i have tried this thing, but still it doesn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.