Delayed group messaging in openfire
Asked Answered
R

1

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();
    }
Revocation answered 28/11, 2016 at 12:20 Comment(7)
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...Ammadis
Its occur only first message Only . Once we switch the group then same happen in first message.Revocation
If it's just database issue then happens same things again and againRevocation
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.Ammadis
I have checked message sent on my to server but delivery receipt getting delayRevocation
try post your code about MUC creationLoretaloretta
@Loretaloretta add muc creation codeRevocation
L
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()

Loretaloretta answered 6/12, 2016 at 11:2 Comment(2)
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...Loretaloretta
look at this answer: #37876039Loretaloretta

© 2022 - 2024 — McMap. All rights reserved.