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?
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?
You define your classes.
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]()
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.
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.
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!
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..."
}
A few things to notice here:
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.
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.
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.