简体   繁体   English

即时消息传递系统的Firebase / NoSQL模式

[英]Firebase/NoSQL schema for an instant messaging system

I'm using Firebase for an app and the built-in real-time capabilities seem well suited for instant messaging. 我正在为应用程序使用Firebase,并且内置的实时功能似乎非常适合即时消息传递。 I'm just having a hard time working out in my head how the database should be set up. 我脑子里很难解决应该如何设置数据库。 Ideally, it's something like this: 理想情况下,它是这样的:

messages: {
  <messageId>: {
    from: <userId>,
    to: <userId>,
    text: <String>,
    dateSent: <Date>
    dateRead: <Date>
  }
}

And that's all fine for sending messages, but reading message threads becomes difficult. 这对于发送消息来说很好,但是读取消息线程变得困难。 I need to query the (potentially huge) list of messages for messages that match the current thread's sender and receiver, and then order those by dateSent . 我需要查询(可能很大)消息列表以查找与当前线程的发送者和接收者匹配的消息,然后按dateSent If that is possible with Firebase's new querying API, then I have yet to figure out exactly how to do it. 如果使用Firebase的新查询API可以做到这一点,那么我还没有确切地知道如何做到这一点。

Querying a huge list of messages is never a good idea. 查询大量消息绝不是一个好主意。 If you want a fast-performing Firebase/NoSQL application, you'll need to model the data to allow fast look up. 如果您想要快速运行的Firebase / NoSQL应用程序,则需要对数据建模以允许快速查找。

In a chat scenario that typically means that you'll model your chat rooms into the data structure. 在聊天场景中,这通常意味着您需要将聊天室建模为数据结构。 So instead of storing one long list of messages, store the messages for each chat "room" separately. 因此,与其存储一长串消息,不如分别存储每个聊天“房间”的消息。

messages
  <roomId>
    <messageId1>: "..."
    <messageId2>: "..."
    <messageId3>: "..."

Now you can access the messages for the chat without a query, just ref.child(roomId).on(... . 现在,您无需查询即可访问聊天消息,只需ref.child(roomId).on(...

If you want a persistent mapping that ensures the same two users end up in the same room, have a look at Best way to manage Chat channels in Firebase 如果您想要一个持久的映射以确保相同的两个用户最终在同一个房间中,请查看在Firebase中管理聊天频道的最佳方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM