I need to build twosome chat, using websockets (socket.io + node.js).
So, the simple example to broadcast message to all users:
socket.on('user message', function (msg) {
socket.broadcast.emit('user message', socket.nickname, msg);
});
But how can I broadcast it from certain user to certain user?
There are two possibilites :
1) Each socket has its own unique ID stored in
socket.id. If you know the ID of both users, then you can simply use2) Define your own ID (for example user’s name) and use
in
connectionhandler. Now whenever you want send message only toJohn, you simply doSide note: the first solution provided cannot be scaled to multiple machines, so I advice using the second one.