1

I know this might be broad, but I have searched the interwebz and haven't found anything at all on this topic.

I am trying to implement a user to user communication system in Angular2. I would like to save the messages on Google Firebase db.

The only requirement is - A user should be able to start a conversation with other user. A conversation can contain multiple messages forth and back between 2 (and only 2) users.

Obviously, I need a collection for the messages on Firestore. enter image description here What should the structure be behind it? Should I have a separate collection inside 'messages' for each of the 'conversations' and then a collection inside for 'individual messages'? Can anyone please give any insight or any best practices how to implement this on a database level?

Thank you.

Paul Strupeikis
  • 275
  • 3
  • 11
  • 1
    I'd recommend the same structure on Firestore that I've used on Firebase' Realtime Database. See http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Nov 16 '17 at 15:42
  • Frank, thank you for your comment. So the structure you're suggesting would be: messages/userId1_userId2 and then store (let's say 3 fields) timestamp, message, and who sent it? – Paul Strupeikis Nov 16 '17 at 16:09
  • Yup. That is a fairly common 1:1 chat model. You'll also want to store `/userchatrooms/userid1` with all their chat room IDs. The chat room name is just a trick to get repeatable, reliable room names cheaply. – Frank van Puffelen Nov 16 '17 at 16:41
  • @FrankvanPuffelen, any suggestions on how to deal with big amounts of messages? If I am storing the whole conversation in `/conversations/user1_user2/messages` array then I need to `stringify` and `parse` to JSON the whole `messages` array which might become quite large. How would you deal with this? – Paul Strupeikis Nov 17 '17 at 12:13
  • 2
    Uhm... did you consider not stringifying the messages? If you store the messages as regular JSON, you can use Firestore queries to get just the number of messages you need. – Frank van Puffelen Nov 17 '17 at 16:24
  • 1
    @Paul Strupeikis could you please post the solution you used? How do you query firestore documents where userid like userid1_% ? – m-bhole Nov 06 '18 at 13:46

0 Answers0