Delay While connecting to XMPP Server in my application

Smack Version:
4.4.0 beta2 but observed with 4.3.4 also

Gradle entries

implementation "org.igniterealtime.smack:smack-android-extensions:4.4.0-beta2"
implementation "org.igniterealtime.smack:smack-tcp:4.4.0-beta2"

The Problem:

I have a connection delay that I cannot find explanations. Basically the connection to the local machine takes almost 1min in the Emulator and 20s+ on the physical device. I have tried everything I know and ended up with the code below. The time on device is now around 15s (after disabling the debug), but it is still a lot for just connecting.

Am not doing anything complex, just connecting.

Anyone with idea what I can do to optimize the delay?

Code:

class XMPPConnectionManager {
    companion object  {
        const val JABBER_DOMAIN = "localhost"
        const val JABBER_URL = "192.168.1.101"
        const val JABBER_RESOURCE = "Resource"
        const val JABBER_PORT = 5222
        const val CONNECTION_TIMEOUT = 300
        const val REPLY_TIMEOUT = 30000L
    }


    private val conn: XMPPTCPConnection by lazy {
        val builder = XMPPTCPConnectionConfiguration.builder()
            .setXmppDomain(JidCreate.domainBareFrom(JABBER_DOMAIN))
            .setHostAddress(InetSocketAddress(JABBER_URL, JABBER_PORT).address)
            .setResource(JABBER_RESOURCE)
            .setCompressionEnabled(true)
            .setSendPresence(false)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) //TODO: Enable it
            //.enableDefaultDebugger()
            .setConnectTimeout(CONNECTION_TIMEOUT)

        val connection = XMPPTCPConnection(builder.build())
        connection.replyTimeout = REPLY_TIMEOUT

        //auto reconnection
        val reconnectionManager = ReconnectionManager.getInstanceFor(connection)
        reconnectionManager.enableAutomaticReconnection()
        reconnectionManager.setReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.RANDOM_INCREASING_DELAY)

        connection.connect()

        return@lazy connection
    }

    fun login(username: String?, password: String?): Boolean {
        try {
            if (!conn.isConnected) {
                conn.connect()
            }

            if(!conn.isAuthenticated){
                conn.login(username, password)
                //Setup OX-IM and other listeners
            }

        } catch (e: Exception) {
            return false
        }
        return true
    }
}

Did you enable debug output to see which stage of the connection stage is taking so long?

My first bet would be that Smack is trying to resolve a DNS entry and is running into a timeout.

DNS resolution issue was one of my first suspects. From what I can guess, it is doing several wrong attempts before getting the right one. I have no proof of this but at least is the only sensible explanation I can come up with.

Things get complicated in that, even if I pass IP instead, nothing improves. I expected things to get faster with IP. No?

Let me run debugger and try to see where it goes banana!

Here are my findings

  • configuration part takes 3secs (second attempt after login failure goes almost immediately)
  • creating connection 2sec i.e. XMPPTCPConnection(builder.build()). Second attempt after login failure goes almost immediately,
  • Connection is almost immediate, i.e conn.connect() have almost zero delay
  • Login user more than 15sec

This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.