Reputation: 42
I have a problem but researched many days but still do not resolve it.
I do project chat user to user socket.io and nodejs.
I am user A, when I click a user B on list user on app, It will let user A and B join the same room (set roomname is (IdOfUserA)_(IdOfUserB)
).
How can i do the same on server-side, please help me or show me the post with similar problem.
Thanks very much.
Upvotes: 0
Views: 1663
Reputation: 111
In pseudo-code I would do something like this :
On the client side
socket.emit('chat-with-user-b', {userb : IdOfUserB});
socket.on('chat-with-me', function(roomID){
// open tchat etc..
});
On the server side :
io.on("connection", function(socket){
socket.on('chat-with-user-b', function(IdOfUserB){
var roomID = IdOfUserB + '_' + socket.id; // create a unique room id
socket.join(roomID); // make user A join room
io.sockets[IdOfUserB].join(roomID); // make user B join room
io.sockets.in(roomID).emit('chat-with-me', roomID); // send join event to both.
});
});
Upvotes: 1