用于聊天应用程序的Firebase数据库结构 [英] Firebase database structure for chat application

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

问题描述

我正在尝试为聊天系统构建一个Firebase数据库.我想要实现的是在用户成功登录后,他们将看到已发送给其他用户的消息列表.每个消息预览将显示最后一条消息.然后,用户可以在消息上选择以查看完整的聊天详细信息.它应该像Facebook Messenger一样工作.

I am trying to structure a firebase database for chat system. What I wanted to achieve is after user successfully logged in, they will see a list of message they have sent to different users. Each of the message preview will show the last message. Then, user can select on the message to view the full chat details. It should work like the Facebook Messenger.

我的设计结构如下:

chatMessage
    sender *(Assume this one is user)*
        threads
            threadID1
                messageID1
                    datetime, content, receiver, status
                messageID2
                    datetime, content, receiver, status
            threadID2
                messageID1
                    datetime, content, receiver, status
                messageID2
                    datetime, content, receiver, status
    sender *(Assume this one is admin)*
         threads
            threadID1
                messageID1
                    datetime, content, receiver, status
                messageID2
                    datetime, content, receiver, status

上面的设计使我知道用户ID1已登录,我可以检索他发送的所有消息.但是,我不知道消息之前是否有任何答复,因此无法检索上一条消息.

The design above allows me to know let's say userID1 logged in, I can retrieve all the messages he sent. However, I am not able to know if there is any reply prior to the message and therefore I am not able to retrieve the last message.

我如何才能真正对其进行重组,以实现上面提到的目标?有什么建议吗?

How can I actually restructure it so that I can achieve what I mentioned above? Any suggestions?

谢谢!

推荐答案

听起来像您要

  1. 在用户之间拥有聊天室"
  2. 显示每个用户的聊天室列表,以及该聊天室的最新消息

如果这是您的要求,我会在您的数据库中对它们进行精确建模.

If those are your requirements, I'd model precisely those in your database.

因此,对于每个聊天室(一组特定用户之间的聊天),为该聊天室的消息建模:

So for each chat room (a chat between a certain set of users), model the messages for that room:

chats: {
  $roomId: {
    $messageId: {
      senderId: "..."
      message: "..."
    }
  }
}

现在,为每个用户分别建模其聊天记录和最新消息:

Now for each user, model a separate list of their chats and the latest message:

userRooms: {
  $uid: {
    $roomId: {
      "message: "..."
    }
  }
}

现在,每当用户将消息发布到会议室时,您都需要将该消息推送到/chats/$roomId ,并且对于该聊天室中的每个用户,将消息写入/userRooms/$uid/$roomId(覆盖在那里退出消息.

Now whenever a user posts a message to a room, you will need to push that message to /chats/$roomId and for each user in that chat room write the message to /userRooms/$uid/$roomId (overwriting the exiting message there).

这种类型的数据复制称为扇出数据,因为您正在数据库中的多个位置散布单个信息片段.它在NoSQL数据库中非常普遍,并且是它们如此之好扩展的部分原因:它们以写复杂性为代价来提高读取性能.

This type of data duplication is known as fanning out data, because you're spreading a single snippet of information over multiple places in the database. It is quite common in NoSQL databases, and is part of the reason they scale so well: they trade write complexity for read performance.

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

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