如何在Firebase实时数据库上为聊天应用程序建模? [英] How to model a chat application on Firebase Realtime Database?
问题描述
我正在尝试为聊天应用程序设计一个结构,包括私人消息和通过使用实时firebase将我分组我不知道如何设计结构任何帮助,请我试图做这样的事情是真的
I am try to design a structure for chat application include private message and groups i by using real time firebase i don't know how to design the structure any help please i am try to do something like this is that true
Users
|
|_USER1
| |
| |__FRIENDS
|
|_USER2
|
|__FRIENDS
推荐答案
您通常最终将在数据库中为聊天室"建模.这样,每次对话(用户在启动应用程序时可能会看到的对话)都显示为某个根列表下的单独节点.您可能希望将每个对话的主要元数据与其实际消息及其参与者分开.
You'll typically end up modeling the "chat rooms" in the database. So that each conversation (that users may see when they start the app), shows up as a separate node under some root list. You may want to separate the main metadata of each conversation from its actual messages and its participants.
所以:
Chats
chat1
title: "...."
lastUpdated: ...
chat2
title: "...."
lastUpdated: ...
Messages
chat1
message1: { ... }
message2: { ... }
chat2
message3: { ... }
message3: { ... }
Participants
chat1
userid1: true
userid2: true
chat2
userid1: true
userid3: true
userid4: true
userid5: true
通过对每个列表中的节点使用相同的键( chat1
, chat2
),您可以在需要时轻松查找数据以进行完整的聊天.但是,通过将它们保存在单独的顶级列表中,可以更好地保护应用程序的安全,并更好地控制加载的数据量.
By using the same keys (chat1
, chat2
) for the nodes in each list, you can easily look up the data for a complete chat when needed. But by keeping them in separate top-level lists, you can better secure the app, and better control how much data is loaded.
另请参阅:
这篇关于如何在Firebase实时数据库上为聊天应用程序建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!