聊天应用程序中的 Firebase 实时数据库结构 [英] Firebase realtime database structure in chat app

查看:27
本文介绍了聊天应用程序中的 Firebase 实时数据库结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉我的英语水平不好,我来自阿根廷.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆