1

I was having some problem with Firebase. What I wanted to do is create a new chatroom if it does not exist, otherwise, get the existing chatroom ID. What I have achieved so far is:

enter image description here

The database structure as such:

chatrooms 
    chatroomID
        participantID1 *(can be senderKey or recipientKey)*
        participantID2 *(can be senderKey or recipientKey)*
        latestMessage

The code where I create the new chatroom:

let promiseRoomKey = new Promise((resolve, reject) => {
        // need to perform check before insert new chatroom

        // create room for chat
        var roomKey = firebase.database().ref('chatrooms').push({
            [senderKey]: true,
            [recipientKey] : true
        }).getKey();

    resolve(roomKey);
    });

The problem is before I create the new chatroom, I need to check if both the senderKey and recipientKey existed in the same chatroom. If both of them appeared in the same chatroom, I grab the chatroomID. If not, then I will proceed to create a new chatroom.

But I have no ideas how can I actually check if both the senderKey and recipientKey appeared in the same chatroom. Any ideas?

Ps. Please ignore the latestMessage node as of now because it is meant to ease my reading purpose only.

Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
QWERTY
  • 2,303
  • 9
  • 44
  • 85
  • Using firebase transactions, you can check for the current value in the database before proceeding with something. See https://firebase.google.com/docs/database/web/read-and-write#save_data_as_transactions – Ayudh Dec 26 '17 at 04:24
  • Typically when you want to ensure a value or combination of values is unique, see if you can put that value/those values into a key. Since keys are guaranteed to be unique in their collection, half your problem is automatically solved. In this case you can determine the chat room ID based on the IDs of the participants in that room. See http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Dec 26 '17 at 04:54
  • @FrankvanPuffelen I see I see. But how can I actually push the concatenated string as the chatroomID? Because by using push() into firebase, it will automatically generate a unique push ID, am I right? Also, by concat the participant IDs as chatroom ID, is it better to concat their unique push account ID or just the username itself? – QWERTY Dec 26 '17 at 05:03
  • Instead of calling `ref.push(...)` use `ref.child(key).set(...)`. – Frank van Puffelen Dec 26 '17 at 05:09

1 Answers1

1

Well, IDK if it's a good solution, but you can create a chatroomID with both (sender and recipient ids) so you need to search in the database if exists any chatroom with both ids.

users
  id001:"User Name"
  id002:"User Name"
chatrooms
  id001id002
    id001
    id002
    latestMessage

To search in the database only once, you can sort the ids, so it'll be always the lower id first, like in the example above.

abagshaw
  • 6,162
  • 4
  • 38
  • 76
Alisson Enz
  • 145
  • 1
  • 8
  • Sorry but you mean create a chatroom ID by concat both the user IDs? Also, after concat the string, how to actually check if the room exist? – QWERTY Dec 26 '17 at 04:55
  • Yea, just concat them. To check just use `firebase.database().ref('chatrooms/id001id002').once('value').then((snapshot) => { // check if snapshot.val() exists })` – Alisson Enz Dec 26 '17 at 20:50