Here is my way to implement one to one chat with two users using socket.io rooms
USERS TABLE
------------
id name
1 bar
2 foo
3 clay
Let's say if user bar
want to chat with foo
socket.join(1);
socket.join(2);
Upon sending messages opposite user id is used as receiver id so that I can easily send messages
socket.on('chat', function(data){
//data object contains other user id as receiver id
io.sockets.in(data.receiverId).emit('chat',{
message:data.message,
created_at:new Date()
});
});
This works fine but I need one more condition at a time a user can chat only once to the user.
if clay
send a message to bar
that message can be readable by bar
, so how can I avoid this problem