Allow Users to Send Messages to Multiple Users Simultaneously in a Messaging App

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

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.

Storing send message history on parse.com for a messaging app?

You could create a Chat object for each conversation, and store every message in an array of pointers. So when your message is sent, you add it to the chat array.

Showing a chat log will then be as easy as querying for the correct Chat object (using includeKey for the pointer array key, which will fetch all the messages together with the Chat object) and then showing the messages directly.

Socket.io Private messaging, with multiple users online at the same time

Take a look in this document. You should specify rooms. Every one will be a "room", when you send a message with event and room specified the correct user will receive it.

Your users object is just a plain object, don't reflect what I'm talking about.

Sending sms to multiple people in android

The net-net-net here is it cannot be done without iterating through a loop and sending 1 message to 1 addressee.

I spent 1/2 a Saturday trying to do this very thing. I could not make it work with ";", ",", " ", or "\n". I should have tried hard-coding 2 addressees separated by all the delimiters first, but I did learn a valuable lesson about the Android SDK: if they wanted you to send to more than 1 addressee at a time then they'd accept an ArrayList or an array of Strings rather than a singular String ;)

protected void sendMsg(Context context, SmsMessage smsMessage) {
SmsManager smsMgr = SmsManager.getDefault();
ArrayList<string> smsMessageText = smsMgr.divideMessage(smsMessage.getMsgBody());
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
int AddresseesPerMessage = 10;
StringBuilder builder = new StringBuilder();
String delim = "";
for (ContactItem c:smsMessage.getAddresseeList()) {
// For every phone number in our list
builder.append(delim).append(c.getPhoneNumber().toString());
delim=";";
if (((smsMessage.getAddresseeList().indexOf(c)+1) % AddresseesPerMessage) == 0 || smsMessage.getAddresseeList().indexOf(c)+1 == smsMessage.getAddresseeList().size()) {
// using +1 because index 0 mod 9 == 0
for(String text : smsMessageText){
// Send 160 bytes of the total message until all parts are sent
smsMgr.sendTextMessage(builder.toString(), null, text, sentPI, deliveredPI);
}
builder.setLength(0);
delim="";
}
}
}


Related Topics



Leave a reply



Submit