0

I'm developing simple chat app with Vue and Firebase. I'm struggling with getting data as I want.

I will have data structure roughly like this:

{    
    user:{
           id:{
               .......
              }
          },
    chats:{
            id:{
                ......
           },
    members:{
             chatId:{
                     userId:{
                             ......
                            }
                     }
             }
}

I wonder how to know if there is chat exist between users. I want to know if there is function that gets chat id(between user A and B) with parameters(user A and B ids).

For instance, when there is chat between user A and B, data would be like this:

{
    user:{
          A:{...}, B:{...}
         },
    chat:{
          chatBetweenAandB:{...}
         },
    members:{
          chatBetweenAandB:{
                            A:{...}, B:{...}
                           }
         },
}

In this case, how can I find chatBetweenAandB with parameters A and B?

김민겸
  • 75
  • 2
  • 8
  • 1
    I recommend naming the chat room keys (things like `chatBetweenAandB` in your structure) after the UID of the users in that chat. See my answer here for more: http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Nov 18 '20 at 23:22
  • @FrankvanPuffelen Thank you for your answer. It seems a good solution, I have another question though, what if I want to make it possible to have group chat as well? In my app's case, users in one to one chat should be able to invite 3rd users. If one to one chat becomes group chat, It seems I have to remove the existing chat and members data and recreate it with new key (I refered this: [link](https://stackoverflow.com/questions/39107274/is-it-possible-to-rename-a-key-in-the-firebase-realtime-database)). Is it not too costly operation? If there were many messages in the chat. – 김민겸 Nov 19 '20 at 01:20
  • There are two common modes: 1) adding a user doesn't allow them to see the history, in which case I'd recommend the same approach as before, 2) adding a user allows them to see all history too, in which case I'd recommend giving the rooms another generated name (not based on the participants) and store group membership as a separate node in a similar way to what I show at the end of my linked answer. – Frank van Puffelen Nov 19 '20 at 01:54

0 Answers0