MQTT, Mosquitto, Apple and Java Issues

As I develop my Mule connector for MQTT, I ran into some issues that kept me from testing real-world scenarios from my Apple laptop and desktop.  Anytime I connect using the Eclipse Paho MqttClient class, and subscribe to a given topic, I will inevitably get a “Connection Lost” exception after two to four minutes of time.  I mitigate this in my Mule connector by auto-reconnecting to the MQTT broker, however, I was baffled at why this was happening.

I started by running the Sample class provided by the Eclipse Paho team.  I pointed this at my local instance of Mosquitto (installed via Homebrew), and subscribed to a topic.  I simply let the program run without publishing any messages to the broker.  After roughly two minutes, I would get disconnected.  Figuring my configuration of Mosquitto was to blame, I pointed this at the public test Mosquitto server, found at  Again, I repeated the same steps with the same results, an exception was generated minutes later for Connection Lost.

Finally, I pointed the Sample class at a different broker all together, which in this case was the public broker.  Same topic subscription and M.O, I connected and subscribed, and waited.  Thirty minutes passed without a single “connection lost” exception.  To hopefully prove network issues, I re-pointed the program at, and again found myself getting bounced in a couple minutes.

I’m at a loss for the cause of this issue, other then something with Mac’s implementation of Java and Mosquitto.  I even tested this using the Oracle Java7 version for Mac, and found the same issue.  Simply changing the broker implementation fixed it, though I can’t believe that’s the issue’s core.

Running this same code against any Mosquitto broker instance from an Ubuntu Server installation has absolutely no issue.  I’ve reproduced this issue only on Apple computers, currently an Apple iMac running Lion and an Apple MacBook Pro running Mountain Lion.

Please tell me that someone has had similar issues and knows how to fix them?  Or can give me something else to try?  I’ve posted the links to the code that I ran to test this below.


About dmiller44

Husband. Father. Son. Programmer. Software Architect. Electronics Geek. View all posts by dmiller44

One response to “MQTT, Mosquitto, Apple and Java Issues

  • Roger

    When a client connects, it sets a keepalive value in seconds on the broker. The broker expects to receive some for of network communication during this time interval. If it hasn’t received anything in keepalive*1.5 seconds, the client will be disconnected. I imagine the default keepalive is 60s, so 60*1.5=90 ~= two minutes. When you set the keepalive to 600, you said it disconnected eventually – presumably after 900 seconds instead.

    For applications that subscribe to topics with QoS=0 and don’t do any publishing, there is no opportunity for messages to go to the server, even if there are messages being delivered to the client.

    The client library should, however, be dealing with this for you. MQTT has the PINGREQ message which a client can send to the broker and is intended to be used to ensure the broker knows that the client is alive. This should be being done automatically, I’d regard it as a bug that it isn’t. I’ve just tested with a subscription and could see PINGREQs and PINGRESPs being exchanged with the broker and the client wasn’t disconnected. This is on Linux and openjdk 6.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: