问题描述
在消息应用程序中,如何将一条消息同时发送给多个朋友?我在一个 Django 问题中读到这个设计是一个 M2M 关系.您定义了 2 个模型(User 和 SentMessage)并且后端创建了第三个对象?
How is a single message sent to several friends simultaneously in messaging applications? I read in a Django question that this design is a M2M relation. You define 2 models (User and SentMessage) and the backend creates a third object?
例如,微信和 Facebook Messenger 允许您选择多个朋友并同时向他们发送一条消息.这是如何在 iOS、Parse 或您自己的 Node.js 后端中完成的?
For example, Wechat and Facebook Messenger allow you to select multiple friends and send a single message to them simultaneously. How is this done in iOS, Parse or your own Node.js backend?
你定义你的类.
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. 具有特定属性的用户,例如["年龄"] 或 ["性别"] 同时.
How would you allow for the sending of messages to:
A. All users simultaneously.
B. Users with specific attributes e.g. ["age"] or ["sex"] simultaneously.
随时为其他服务器贡献解决方案.
Feel free to contribute solutions for other servers.
推荐答案
在 Firebase 中,您也可以使用第三个表"对多对多关系进行建模,您可以在其中将实体 1 的项目连接到实体 2 的项目.有关更多信息,请参阅Firebase 中的多对多关系
In Firebase you model many-to-many relationships with a third "table" too, where you connect items from entity1 one to items of entity2. For more on this see Many to Many relationship in Firebase
但在聊天应用程序的情况下,我通常会以不同的方式对此用例进行建模.向一组用户发送消息通常会在这些应用程序中启动一个临时聊天室.如果其中一个用户回答,则该回答会发给组中的其他人.因此,您实际上已经建立了一个临时聊天室,其中的人可以识别该聊天室.
But in the case of a chat app, I'd typically model this use-case differently. Sending a message to a group of users typically starts an ad-hoc chat room in those apps. If one of the users answers, that answer goes to everyone else in the group. So you've essentially started a temporary chat room, one that is identified by the people in it.
我通常建议在 Firebase 中以参与者的名字命名这个临时聊天室.有关更多信息,请参阅:http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase.在那个模型中,如果你和我开始聊天,我们的房间将是:uidOfMat_uidOfPuf
.所以我们的 JSON 看起来像:
I typically recommend naming this ad-hoc chat room in Firebase after its participants. For more on this, see: http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase. In that model, if you and I start a chat our room would be: uidOfMat_uidOfPuf
. So our JSON would look like:
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..."
}
由于聊天室是由参与者定义的,所以无论何时你和我聊天,我们都会在同一个聊天室中结束.很方便!
Since the chat room is defined by its participants, any time you and I chat, we end up in this same chat room. Quite handy!
现在假设我通过将某人拉入聊天室来寻求帮助来回答您的问题.由于聊天室由其参与者定义,因此添加新参与者会创建一个新聊天室:uidOfMat_uidOfPuf_uidOfThird
.所以我们最终得到:
Now say that I ask someone for help answering your question by pulling them into the chat. Since the chat room is defined by its participants, adding a new participant creates a new chat room: uidOfMat_uidOfPuf_uidOfThird
. So we end up with:
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
聊天室,这将再次创建一个新聊天室.
In the model we've used so far, if we'd add yet another person to the
uidOfMat_uidOfPuf_uidOfThird
chat room, that would again create a new chat room.
许多聊天应用程序都为您提供了为群聊室命名的选项.在许多情况下,将用户添加到这样一个命名的聊天室确实可以让他们访问消息历史记录.我倾向于将此类房间称为持久聊天室,因为它们可以访问历史聊天消息.
Many chat apps give you the option to name a group chat room. In many cases adding a user to such a named chat room does give them access to the message history. I tend to refer to such rooms as persistent chat rooms, since they give access to the historical chat messages.
在上面的示例中,假设我们将 1:1 房间命名为model_chat_room".这意味着将第三人添加到房间后,他们可以立即访问我们的消息历史记录.
In our above sample, say that we'd named our 1:1 room "model_chat_room". That would mean that adding the third person to the room, would have given them access to our message history straight away.
一方面这很方便,因为我不必重复您的问题.另一方面,马特也可以看到我们的整个对话历史.许多人认为 1:1 聊天对话是私密的,这就是为什么持久聊天室"模型通常只适用于 3 个或更多参与者的命名聊天.
On the one hand that is handy, because I wouldn't have had to repeat your question. On the other hand, Matt could have also seen our entire conversation history. Many people consider 1:1 chat conversations private, which is why the "persistent chat room" model is usually only followed for named chats with 3 or more participants.
这篇关于允许用户在消息应用程序中同时向多个用户发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!