0
votes

i create something like chat,i know how to send messages to all user in room with(sails.sockets.broadcast(...e.m)) but can't understand how to send message to one user(i think chat between 2 users), when second user send message to first user, first get the message sended by second, but when i create 3 users, and when third user send message to first - the second users too get the message, how can i do this. Who knows how to do it?

1

1 Answers

4
votes

Socket.io has rooms built in. You could do something like:

io.on('connection', function(socket){
  socket.join('some room');
});

and then call it with :

io.to('some room').emit('some event');

Alternatively, you could pass in an object along with the emit on your server side, and then on your client side, check to see if that object exists. For example:

var obj = {
    shouldDisplay: someIDhere
}

socket.emit('yourEvent', obj);

And then check if the incoming event's object has the ID if the user you want to display the message