0

I am building a chat app using Firebase. I have designed a database structure, but would like to get some insight on it whether it could be structured better.

Here is how it looks:

incoming_messages:
    userID:
        sender_userID:
            -KOkGD9n-ysFqX2DtyKp:
                body: "Hello world!"
                timestamp: 124245345346
            -KOfHTD9n-xsEqX289xGc:
                body: "My second message!"
                timestamp: 124245345346

Every user is observing their node (by userID) of the incoming_messages for updates. The conversations are identified by who sent them to the user, while messages have auto IDs

Same goes for the sent messages of a particular user, and who he sent it to:

sent_messages:
        userID:
            receiver_userID:
                -KOkGD9n-ysFqX2DtyKp:
                    body: "Hello!"
                    timestamp: 124245345346
                -KOfHTD9n-xsEqX289xGc:
                    body: "Haha cool!"
                    timestamp: 124245345346
Balázs Vincze
  • 3,550
  • 5
  • 29
  • 60
  • I'd model the chat rooms as a single list and merge the participants into the room ID/key. See http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Jan 24 '17 at 12:41
  • Thanks! I am afraid though, that I don't understand how one would be observing for new chat rooms. Could you explain that a little? – Balázs Vincze Jan 24 '17 at 12:48
  • Ah, you're looking for how to announce a new chat room. That would indeed require a user-specific node (or all rooms to be listed in a publicly readable node). – Frank van Puffelen Jan 24 '17 at 13:04
  • Yes, let's say that user 1 wants to message user2 . User 2 would know he got a message if he is observing the user1-user2 chanel. But what if user 3 send a message to him. How would he know that now he needs to be observing the channel user2-user3 as well? – Balázs Vincze Jan 24 '17 at 13:34
  • @FrankvanPuffelen Any ideas on this? Thanks! – Balázs Vincze Jan 24 '17 at 22:16
  • As I said in my last comment, you'd need a "user-specific announcement/invite node" for that. `/invites/$uidThatWasInvites/$uidThatSentInvite: true`. – Frank van Puffelen Jan 25 '17 at 06:18

0 Answers0