iosswiftfirebaseparse-platforminstant-messaging

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


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.


Solution

  • 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: