user3441828
user3441828

Reputation: 31

how to create private chat between two users with socket.io and sailsjs

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?

Upvotes: 0

Views: 1133

Answers (1)

Alex J
Alex J

Reputation: 1019

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

Upvotes: 4

Related Questions