聊天应用程序中的Firebase实时数据库结构 [英] Firebase realtime database structure in chat app
问题描述
对不起,我的英语水平不好,我来自阿根廷.
sorry for my bad English level, I'm from Argentina.
我在Firebase中具有以下消息数据结构:
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?"
其中 -KezmqXSdKCNFFA432Uc , -KfCEwklG_y3naRDIUiY , -KezFDSAADFASFFS3221 和 -KASDF32324SDFASD1FS 是用户的地方.
Where -KezmqXSdKCNFFA432Uc, -KfCEwklG_y3naRDIUiY, -KezFDSAADFASFFS3221 and -KASDF32324SDFASD1FS are users.
我的问题是,我在消息"节点中创建了childEventListener来接收新用户的消息,但是我正在接收所有用户的所有新消息(我在每个应用程序中登录了一个用户),因为我的childListener位于消息"节点.
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.
如果添加一条消息时我有1000个用户,那么新消息到达1000个用户是否正确? (假设您在应用程序中,可以检查该消息属于哪个用户.)
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).
谢谢!
推荐答案
如果您执行类似以下的结构:
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
您可以将侦听器附加到/userChats/userUID(将显示活动聊天),将侦听器附加到/chatMessages/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.
通过这种方法可以更轻松地设置Firebase安全规则,并且用户仅会收到自己所在的聊天消息.
This way is a lot easier to setup firebase security rules, and users will only receive chat messages which they are apart of.
这篇关于聊天应用程序中的Firebase实时数据库结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!