Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
56
rated 0 times [  62] [ 6]  / answers: 1 / hits: 17189  / 9 Years ago, thu, november 5, 2015, 12:00:00

In my main page I have a list of users and i'd like to choose and open a channel to chat with one of them.



I am thinking if use the id is the best way and control an access of a channel like USERID1-USERID2.



But of course, user 2 can open the same channel too, so I'd like to find something more easy to control.



Please, if you want to help me, give me an example in javascript using a firebase url/array.



Thank you!


More From » arrays

 Answers
28

A common way to handle such 1:1 chat rooms is to generate the room URL based on the user ids. As you already mention, a problem with this is that either user can initiate the chat and in both cases they should end up in the same room.


You can solve this by ordering the user ids lexicographically in the compound key. For example with user names, instead of ids:




var user1 = Frank; // UID of user 1
var user2 = Eusthace; // UID of user 2

var roomName = 'chat_'+(user1<user2 ? user1+'_'+user2 : user2+'_'+user1);

console.log(user1+', '+user2+' => '+ roomName);

user1 = Eusthace;
user2 = Frank;

var roomName = 'chat_'+(user1<user2 ? user1+'_'+user2 : user2+'_'+user1);

console.log(user1+', '+user2+' => '+ roomName);

<script src=https://getfirebug.com/firebug-lite-debug.js></script>




A common follow-up questions seems to be how to show a list of chat rooms for the current user. The above code does not address that. As is common in NoSQL databases, you need to augment your data model to allow this use-case. If you want to show a list of chat rooms for the current user, you should model your data to allow that. The easiest way to do this is to add a list of chat rooms for each user to the data model:


"userChatrooms" : {
"Frank" : {
"Eusthace_Frank": true
},
"Eusthace" : {
"Eusthace_Frank": true
}
}

If you're worried about the length of the keys, you can consider using a hash codes of the combined UIDs instead of the full UIDs.




This last JSON structure above then also helps to secure access to the room, as you can write your security rules to only allow users access for whom the room is listed under their userChatrooms node:


{
"rules": {
"chatrooms": {
"$chatroomid": {
".read": "
root.child('userChatrooms').child(auth.uid).child(chatroomid).exists()
"
}
}
}
}

[#64496] Tuesday, November 3, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harleew

Total Points: 70
Total Questions: 87
Total Answers: 117

Location: Namibia
Member since Mon, Feb 21, 2022
2 Years ago
;