允许用户在消息传递应用程序中同时向多个用户发送消息

Allow users to send messages to multiple users simultaneously in a messaging app

在消息应用程序中如何将一条消息同时发送给多个朋友?我在 Django 问题中读到这个设计是 M2M 关系。您定义了 2 个模型(User 和 SentMessage)并且后端创建了第三个对象?

例如,微信和 Facebook Messenger 允许您 select 多个朋友并同时向他们发送一条消息。这是如何在 iOS、Parse 或您自己的 Node.js 后端完成的?

你定义你的类.

user["username"] = String
user["sex"] = String
user["age"] = Int

///

let messageObj = PFObject(className: "Messages")   
messageObj["sender"] = PFUser.current()?.username
messageObj["message"] = messageTextView.text
messageObj["likes"] = [String]()

您如何允许将消息发送至:
A. 所有用户同时。
B. 具有特定属性的用户,例如["age"] 或 ["sex"] 同时。

欢迎为其他服务器贡献解决方案。

在 Firebase 中,您也使用第三个 "table" 为多对多关系建模,您将实体 1 中的项目连接到实体 2 中的项目。有关更多信息,请参阅

但对于聊天应用程序,我通常会以不同的方式对此用例建模。向一组用户发送消息通常会在这些应用程序中启动一个临时聊天室。如果其中一位用户回答,则该答案将发送给组中的其他所有人。因此,您基本上已经启动了一个临时聊天室,一个由其中的人识别的聊天室。

我通常建议在 Firebase 中以参与者的名字命名这个临时聊天室。有关更多信息,请参阅:。在那个模型中,如果你和我开始聊天,我们的房间将是:uidOfMat_uidOfPuf。所以我们的 JSON 看起来像:

chats: {
  "uidOfMat_uidOfPuf": {
    -Labcdefgh1: {
      sender: "uidOfMat",
      text: "How is a single message sent to several friends simultaneously in messaging applications?"
    }
    -Labcdefgh2: {
      sender: "uidOfPuf",
      text: "In Firebase you model many-to-many relationships with a third "table" too..."
    }

由于聊天室是由参与者定义的,任何时候你我聊天,我们都会进入同一个聊天室。非常方便!

现在假设我通过将某人拉入聊天来请他们帮助回答你的问题。由于聊天室是由其参与者定义的,因此添加新参与者会创建一个新聊天室:uidOfMat_uidOfPuf_uidOfThird。所以我们最终得到:

chats
  uidOfMat_uidOfPuf
    -Labcdefgh1: {
      sender: "uidOfMat",
      text: "How is a single message sent to several friends simultaneously in messaging applications?"
    }
    -Labcdefgh2: {
      sender: "uidOfPuf",
      text: "In Firebase you model many-to-many relationships with a third "table" too..."
    }
  }
  "uidOfMat_uidOfPuf_uidOfThird": {
    -Labcdefgh3: {
      sender: "uidOfPuf",
      text: "Hey Third. Puf here. Mat is wondering how to send a single message to several friends simultaneously in messaging applications. Do you have an idea?"
    }
    -Labcdefgh4: {
      sender: "uidOfThird",
      text: "Yo puf. Long time no see. Let me think for a moment..."
    }

这里有几点需要注意:

  • 在我们目前使用的模型中,如果我们将另一个人添加到 uidOfMat_uidOfPuf_uidOfThird 聊天室,那将再次创建一个新的聊天室。

  • 许多聊天应用程序都允许您为群聊室命名。在许多情况下,将用户添加到这样的命名聊天室 确实 允许他们访问消息历史记录。我倾向于将此类房间称为 永久聊天室 ,因为它们可以访问历史聊天消息。

  • 在我们上面的示例中,假设我们将 1:1 房间命名为 "model_chat_room"。这意味着将第三个人添加到房间中,可以让他们立即访问我们的消息历史记录。

  • 一方面这很方便,因为我不必重复你的问题。另一方面,马特也可以看到我们的整个对话历史。许多人认为 1:1 聊天对话是私密的,这就是为什么 "persistent chat room" 模型通常仅用于 3 名或更多参与者的命名聊天。