10
votes

I have built a chat application using an Openfire (xmpp) server. One-to-one person chats are working fine and the messages are delivered instantly. But when we send a message inside a group, the first message gets delayed and the second message is delivered instantly.

MultiUserChatManager groupChat =
          MultiUserChatManager.getInstanceFor(connection).getMultiUserChat("group_name");
groupChat.send("Message object");

Why is the first message getting delayed?

MUC Creation is

 MultiUserChatManager mchatManager = MultiUserChatManager.getInstanceFor(xmpptcpConnection);
      MultiUserChat mchat = mchatManager.getMultiUserChat(group);
      if (!mchat.isJoined()) {
        Log.d("CONNECT", "Joining room !! " + group + " and username " + username);
        boolean createNow = false;
        try {
          mchat.createOrJoin(username);
          createNow = true;
        } catch (Exception e) {
          Log.d("CONNECT", "Error while creating the room " + group + e.getMessage());
        }

        if (createNow) {

          Form form = mchat.getConfigurationForm();
          Form submitForm = form.createAnswerForm();

          List<FormField> formFieldList = submitForm.getFields();
          for (FormField formField : formFieldList) {
            if(!FormField.Type.hidden.equals(formField.getType()) && formField.getVariable() != null) {
              submitForm.setDefaultAnswer(formField.getVariable());
            }
          }

          submitForm.setAnswer("muc#roomconfig_persistentroom", true);
          submitForm.setAnswer("muc#roomconfig_publicroom", true);

          mchat.sendConfigurationForm(submitForm);

          //mchat.sendConfigurationForm(
          //    new Form(DataForm.Type.submit)); //this is to create the room immediately after join.
        }
      }
      Log.d("CONNECT", "Room created!!");
      return true;
    } catch (SmackException e) {
      e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
      e.printStackTrace();
    }
1
Maybe you store the incoming message inside a database? If yes, I guess it´s more a problem by creating the database than a delayed message...Opiatefuchs
Its occur only first message Only . Once we switch the group then same happen in first message.Surya Prakash Kushawah
If it's just database issue then happens same things again and againSurya Prakash Kushawah
Yep, that´s what I am thinking of. I guess that you create a new database and/or the table if the first message is send, and here it could be any problem. But it´s just an assumption, I never faced a problem like this with MUC. Another scenario is that the user login (to the group) needs much time.Opiatefuchs
I have checked message sent on my to server but delivery receipt getting delaySurya Prakash Kushawah

1 Answers

3
votes

There's an issue about creation and a kind of side-effect propagated on sending.

I think simply that you need to join the chat the first time since you didn't before and the first message also activate the Groupchat on server, so the first message it's delayed because you didn't finalized the multiuserchat creation.

How to fix.

In creation phase, this part must be improved:

if (!mchat.isJoined()) {
        Log.d("CONNECT", "Joining room !! " + group + " and username " + username);
        boolean createNow = false;
        try {
          mchat.createOrJoin(username);
          createNow = true;
        } catch (Exception e) {
          Log.d("CONNECT", "Error while creating the room " + group + e.getMessage());
        }

With just:

boolean createNow
try
{
   if (!mchat.isJoined())
   {
       createNow = mchat.createOrJoin(username);
   }
}
catch (Exception e)
{
  throw new Exception("ERROR!");
}

and after this invokation:

mchat.sendConfigurationForm(submitForm);

add:

if (!mchat.isJoined()) {
  mchat.join(username);
}

creationOrJoin method it's about creation OR join (as name says): to activate the chat, you must join it after the creation phase.

However createOrJoin has maybe an unexpected behaviour due a double check about already joined rooms to keep syncro between session in client and session on server, so the mchat.join() must be invoked after. An explicit name can sounds like: mustCreateBeforeOrCanJoinDirectly()