Firebase数据库中的多用户聊天室数据结构 [英] Multi-users chat room data structure in Firebase Database

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

问题描述

我很难确定如何正确构建多用户聊天室 FirebaseDatabase 体系结构.

I'm having hard time figuring out how to structure multi users chat room FirebaseDatabase architecture properly.

基本上,该应用程序支持用户之间的私人消息(可以在2个用户之间,也可以在多个用户之间).如果仅在2个用户之间,则数据结构确实非常容易.

Basically, the app supports private messages between users (could be between 2 users or multiple users). If it was between 2 users only, the data structure is quite easy really.

我正在尝试降低成本(当当前登录的用户正在与某人聊天并且观察者正在获取与该特定聊天无关的所有消息时,我不希望这样做)并适当地构造数据.如果仅在2个用户之间,则我总是可以在当前用户ID下添加接收者,并仅查询该节点.但是,对于多个用户(甚至不确定其中有多少个用户),我需要依靠聊天室ID,但是我很难过.

I'm trying to reduce the cost (I don't want when the current logged user is having a chat with someone and the observer is getting all the messages that are no related to this particular chat) and structure the data properly. If it was between 2 users only, I could always add the recepient under the current user ID and query only that node. But with multiple users (not even sure how many of them) I need to rely on a chat room Id but I'm having hard time with that.

所以,这就是想法,假设用户选择了另一个聊天对象.我在想这样的事情:

So, here's the idea, let's say the user selects another to start a chat with. I was thinking about something like this:

我所面临的问题是维护该 chatId .每次用户要发送新消息时,如何根据 chatId 检查是否已经有与相同收件人(或多个收件人)进行的聊天.这对我来说是令人困惑的部分.我已经坚持了好几天,无法找到正确的方法来维护该 chatId 并对其进行正确的查询.欢迎任何帮助.

The problem that I'm facing with this is maintaining that chatId. Every time the user is about to send a new message, how do I check if there's already a chat with the same recepient (or recepients) based on the chatId. This is the confusing part for me. I'm stuck on this for days now and I can't figure out a proper way to maintain that chatId and query it properly. Any help is welcome.

推荐答案

如果我理解这个问题,则该应用有多个用户,并且这些用户可能会以1-1或1的身份进行多次聊天,并且您要避免用户收到有关他们不参与的聊天.

If I understand the question, the app has multiple users and those users could have chats 1-1 or 1-many and you want to avoid users getting events for chats they are not part of.

让我们从一个用户节点开始

Let's start with a users node

users
  uid_0
    name: "Bill"
    chats:
      chat_id_0: true
      chat_id_9: true
  uid_1
    name: "Jesse"
    chats:
      chat_id_0: true
      chat_id_6: true
  .
  .
  .

然后是一个简化的聊天节点

and then a simplified chats node

chats
   chat_id_0:
     uid_0: "Hey there, Jesse"
     uid_1: "Sup Bill?"
   chat_id_6:
     uid_0: "This is Bill, anyone around?"
   chat_id_9:
     uid_1: "Look ma, no hands"
     uid_7: "Impressive"

在上面,当用户开始聊天时,使用.childByAutoId创建chat_id_x.

In the above, chat_id_x is created with .childByAutoId when the user starts a chat.

当Bill登录(uid_0)时,读取其聊天节点,然后将观察者添加到该节点内列出的聊天中.在这种情况下,Bill是chat_id_0和chat_id_9的一部分.对属于0和6的Jesse执行相同的过程.

When Bill logs in (uid_0) read in his chats node and then add observers to the chats listed within that node. In this case, Bill is part of chat_id_0 and chat_id_9. Do the same process for Jesse who is part of 0 and 6.

Bill和Jesse都在chat_id_0中聊天,Bill正在等待所有也在观看chat_id_6的用户的答复,并且Jesse在chat_id_9中与uid_7进行了交谈.

Both Bill and Jesse are having a chat in chat_id_0, Bill is awaiting a reply from any users also watching chat_id_6 and Jesse is conversing with uid_7 in chat_id_9.

在任何时候都有一个事件(例如,将新消息添加到聊天中时,.childAdd),只有那些观察该聊天(已订阅")的用户才会收到事件.

From there any time there's an event (.childAdded when a new message is added to a chat for example) only those users observing that chat ('subscribed') will receive an event.

显然,每个聊天节点(时间戳等)中将包含更多详细信息

Obviously more detail would be included in each chat node (timestamp etc)

我如何检查是否已经有与同一收件人的聊天(或收件人)基于chatId

how do I check if there's already a chat with the same recepient (or recepients) based on the chatId

有几种解决方法;

一个是,当将观察者添加到每个聊天中时,您是(.childAdded)的一部分,该聊天节点中的每个子节点都将返回到您的应用中.这对两件事有好处-一是用现有的聊天消息填充UI,其二...然后,您将知道与该聊天有关的人.将这些UID保留在具有聊天ID的数组中,并在将聊天发送给该人之前,先查看它们是否以现有聊天的形式存在于数组中.

One is that when an observer is added to each of the chats you are part of (.childAdded) each child node within that chat node will be returned to your app. This is good for two things - one is to populate the UI with the existing chat messages and secondly... you will then know who was involved with that chat. Keep those UID's in an array with the chat id, and before sending a chat to that person see if they exist in the array as an existing chat.

第二个选项是查询用户uid出现的所有聊天(使用.observeSingleEvent进行查询).这将返回他们所属的所有聊天节点,然后返回他们与谁聊天的uid以及聊天ID.

A second option is to query for all the chat's the users uid appears in (query using .observeSingleEvent). That will return all of the chat nodes they belong to and then have the uid's of who else they've chatting with and the chat id's as well.

另一种选择是在创建聊天时/chat_id_0,添加一个名为/who_dat的子节点,该子节点保留了邀请参加聊天的人员的列表

Another option is when the chat is created, /chat_id_0, add a child node called /who_dat that keeps a list of who was invited to the chat

chats
  chat_id_0
     who_dat
        uid_0: true
        uid_1: true
        uid_2: true
     messages:
        uid_0: "Hey there, Jesse"
        uid_1: "Sup Bill?"

在这种情况下,uid_2也已添加到chat_id_0中,但尚未收到他们的来信.

in this case uid_2 was also added to chat_id_0 but haven't heard from them yet.

这篇关于Firebase数据库中的多用户聊天室数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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