AnsweredAssumed Answered

Connection unstable in Smack

Question asked by L on Nov 7, 2015
Latest reply on Nov 17, 2015 by L

Hello,

 

I am working on an Android app that integrates an XMPP chat, and for those I chose to use Smack (awesome library btw).

Unfortunately, my connection seems rather buggy and unstable.

 

For instance I have the following code to create a connection:

 

final XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
  .setUsernameAndPassword(user.getUsername(), user.getJabberPassword())
  .setServiceName("app.buur.nu")
  .setHost("app.buur.nu")
  .setPort(5222)
  .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) // for testing purposes
  .setDebuggerEnabled(true)
  .build();

 

Next, I have some code to for example listen to incoming messages:

 

ChatManager chatmanager = ChatManager.getInstanceFor(xmppManager.getConnection());
Chat chat = chatmanager.createChat(otherJabberId);
chat.addMessageListener(new ChatMessageListener() {
    @Override
    public void processMessage(Chat chat, Message message) {
        String body = message.getBody();
        Log.e("message trigger", message.getBody());
        ...
    }
});

 

However, sometimes I do not get messages in my message listener, even though the following is visible in the log:

10-31 15:41:51.264 28889-28993/com.lfdversluis.buurapp D/SMACK: RECV (0): <message to="test@app.buur.nu/Smack" type="chat" id="53" from="testje@app.buur.nu/Gajim"><body>test</body><request xmlns="urn:xmpp:receipts"/><thread>277945c1-772a-4d4b-8e1a-274153cfb8a6</thread></message>

 

So I am wondering why my chatmessagelistener is not triggering.

 

Another Issue I have is not being able to send messages.

Here, I have the chat setup as above and use the following code to send a message:

 

 try {
    chat.sendMessage(text.trim());
    DataBaseManager db = new DataBaseManager(ChatActivity.this);
    db.addMessageToDB(model);

    addMessageToScreen(newMessage);

} catch (SmackException.NotConnectedException ignored) {
    XMPPManager manager = XMPPManager.getInstance();
    manager.reconnect(); // Maybe we need to reconnect due to an interrupt and retry..
    try {
        chat.sendMessage(text.trim());
        DataBaseManager db = new DataBaseManager(ChatActivity.this);
        db.addMessageToDB(model);
        addMessageToScreen(newMessage);
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
        Toasteroid.show(ChatActivity.this, "Could not send message. Please try again.", Toasteroid.STYLES.ERROR);
    }
}

 

And the toast with the "could not send message" pops-up every now and then. So apparently I am not connected and I cannot reconnect either?

 

So how can I make my connection more stable and make sure my messages get received?

Outcomes