Firebase realtime database structure in chat app

2019-03-17 01:45发布

问题:

sorry for my bad English level, I'm from Argentina.

I have the following messages data structure in Firebase:

"messages"
   "-KezmqXSdKCNFFA432Uc___-KfCEwklG_y3naRDIUiY"
         "messageDate": "20170620"
         "messageTime": "18:44" 
         "message": "Hi"
   "-KezFDSAADFASFFS3221___-KASDF32324SDFASD1FS"
         "messageDate": "20170620"
         "messageTime": "22:23" 
         "message": "How are you?"

Where -KezmqXSdKCNFFA432Uc, -KfCEwklG_y3naRDIUiY, -KezFDSAADFASFFS3221 and -KASDF32324SDFASD1FS are users.

My problem is that I created a childEventListener in "messages" node to receive new users messages but I am receiving all the new messages of all the users (I'm logged in one user per app) because my childListener is in "messages" node.

Is it correct that if I have 1000 users when adding a message, a new message reaches the 1000 users? (Assuming that within the app, you can check to which user that message belongs).

Thanks!

回答1:

If you do a structure like similar to this:

-chats
   - chatUID
       - members
           - userUID
       - lastMessageSent:messageUID
       - ... more properties  

-chatMessages
   - chatUID
     - messageUID
         - sentBy: userUID
         - messageDate:""
         - messageTime:""
         - message:""

-userChats
    - userUID
       - chatUID

you can attach a listener to /userChats/userUID, which will display active chats, and a listener to /chatMessages/chatUID, which will get all chat messages for a specific chat conversation.

This way is a lot easier to setup firebase security rules, and users will only receive chat messages which they are apart of.



回答2:

In order to structure your database, please read this post: Structuring your Firebase Data correctly for a Complex App. You'll find here for sure the answer to your question.

As a conclusion, try to flatten(denormalize) your database as much as possible.

Hope it helps.



回答3:

{
    "users": {
        "userId": {
            "conversations": {
                "conversationId": {
                    "unseenCount": 0
                },
                "conversationId2": {
                    "unseenCount": 3
                }
        }
    },
    "conversations": {
        "conversationId": {
            "displayedMessage": "Message",
            "members": {
                "userId1": true,
                "userId2": true
            },
            "messages": {
                "messageId": {
                    "type": "text",
                    "text": "Hello",
                    "createdAt": "",
                    "senderId": "userId",
                    "status": "sent",
                    "payload": ""
                }
            },
            "lastMessage": "my last message"
        }
}

}



回答4:

this structure doesn't support what you want to do, it better to change it by using something like channels, where a channel contains the messages between two persons, so when any one of them send a message the other one will be notified.



回答5:

I think this will be the best structure for it:

{
  messages: {
    A8Fcn28ak9ask46: {
      chat_id: "combination of sender and receivers number",
      sender_id: "person sending the message", 
      receiver_id: "person send it to",
      text: "message that the user types",
      timestamp: "123981849404"
    },
    ...
  }
 }

then when u get the results, you can filter through the chat_id's in forward and in reverse, which will get the conversation between two people.

Hope it helps.