Reputation: 5083
I'm trying to make a google/facebook chat clone with socket.io.
When a user connects to server I put the user in a room named after the user ID (so if user have multiple tabs open, all goes to same room and receive message in all tabs). When A send message to B, I broadcast it to room named after B. In UI I have each chat window with ID of the sender so when a user receive message, I find the window with ID of the sender and update the window with new message. Simple.
Now, A and B invites C and D to join in group chat. So the chat turns into a group chat. I'm stuck here. How do I handle such scenario? How do I keep track of chat in client UI and in server?
Upvotes: 0
Views: 2046
Reputation: 5480
You could always name groups as a concatenation (with some delimiter) of the users in the chat ordered by some arbitrary value (such as id). So if you have User A with ID USERA and User B with ID USERB, the room containing them would be USERA-USERB. (Note this schema would only work if userIDs do not have dashes in them). When User C joins the chat open a room with ID USERA-USERB-USERC, add all the users to the room, and rebroadcast the messages from the previous conversation back to the users.
Note this would open a new conversation for UserA and UserB, which you may not want to do. In this case, you would have to find a way of shuffle the rooms in order to change the rooms ID (you'd have to put some thought into this), and then rebroadcasting the last x messages to the new user.
Another note, you will have to send associated room id with all the events that are sent to the clients so that the client knows which chat the event is in relation to.
Upvotes: 0