10

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();
    }
Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42

1 Answers1

3

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()

MrPk
  • 2,862
  • 2
  • 20
  • 26
  • restart Openfire if you can and try with a brand new chat. If is not working again, we can check something different (however I suggest to use my snippet). I have no issue about first message at all... – MrPk Dec 06 '16 at 11:24
  • look at this answer: http://stackoverflow.com/questions/37875539/getting-xmpperrorexception-xmpperror-forbidden-auth-error-while-creating/37876607#37876607 – MrPk Dec 12 '16 at 09:58