Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » MQTT Client has lost connection(Connection does not re-establish)
MQTT Client has lost connection [message #1709644] Wed, 30 September 2015 00:21 Go to next message
Edward Woodhouse is currently offline Edward WoodhouseFriend
Messages: 12
Registered: September 2015
Junior Member
I have MQTT running in an Android service in foreground and background. It is important that the service is available as long as there is network connectivity.

I have left the device in strong signal overnight, it stays connected for 10+ hours just fine and when I disabled and enable wifi, it re-acquires connection as expected.

However, as I move about over the course of the day I inevitably get the message "MQTT Client has lost connection" and subsequnetly I will be unabled to publish or receive anything until relaunching the application.

My theory is that as the Mobile network type changes between NETWORK_TYPE_LTE/NETWORK_TYPE_LTE etc, the ip or connection is for some reason no longer recognised either at server or client end and the client makes no effort to reconnect?

Is the library supposed to handle this? Does anyone know what is going on here or how to fix?
Re: MQTT Client has lost connection [message #1709724 is a reply to message #1709644] Wed, 30 September 2015 14:34 Go to previous messageGo to next message
Ian Craggs is currently offline Ian CraggsFriend
Messages: 83
Registered: March 2013
Member
No, the library makes no attempt to automatically reconnect. The way it was intended to be handled is to make a connect call in the connectionLost callback. You could take this approach today.

I agree that this is what I would expect an Android service to do, which is why we have it on the to-do list.
Re: MQTT Client has lost connection [message #1709758 is a reply to message #1709724] Wed, 30 September 2015 17:31 Go to previous messageGo to next message
Edward Woodhouse is currently offline Edward WoodhouseFriend
Messages: 12
Registered: September 2015
Junior Member
Thank you for the response. I have noted the connectionLost callback you mentionned.

I have implemented an android.net.conn.CONNECTIVITY_CHANGE broadcast receiver for
ConnectivityManager.TYPE_WIFI
ConnectivityManager.TYPE_MOBILE

Here i create a new connectAction whenever a WIFI or MOBILE signal comes online, with priority given to new WIFI networks.

It is fully functional and when I manually disable and enable either WIFI or MOBILE data myself, connection is regained properly.

However, I experience the "MQTT Client has lost connection" when I am solely using MOBILE data as I travel. I am curious if anyone knows what sort of MOBILE data event could be triggering this behaviour? (for example, changing network type? or something else detectable by a PhoneStateListener?).

[Updated on: Wed, 30 September 2015 17:32]

Report message to a moderator

Re: MQTT Client has lost connection [message #1710118 is a reply to message #1709758] Sun, 04 October 2015 16:41 Go to previous messageGo to next message
Edward Woodhouse is currently offline Edward WoodhouseFriend
Messages: 12
Registered: September 2015
Junior Member
For anyone interested in making the MQTT service restablish itself correctly and without losing cleansession messages, here is what to do:

Copy the Android Service src directly into your project and remove any existing dependencies on it. Do not change the folder structure or it will not work. Test that the Android service you imported is being used.

For example by refactoring the service name successfully in the manifest <service android:name="org.eclipse.paho.android.service.MqttService2"></service>

Make MqttService public and MqttService.connections public static

Finally in MqttCallbackHandler's connectionLost() method add this snippit

for (MqttConnection client : MqttService.connections.values()) {
client.reconnect();
}

Hopefully it helps you

[Updated on: Sun, 04 October 2015 16:44]

Report message to a moderator

Re: MQTT Client has lost connection [message #1712380 is a reply to message #1710118] Fri, 23 October 2015 12:25 Go to previous message
Vitalii Stakhov is currently offline Vitalii StakhovFriend
Messages: 3
Registered: October 2015
Junior Member
Hey Edward,

how has your solution worked over time? Any comments why it works?
Previous Topic:Connection lost on first ping after switching to wifi
Next Topic:MqttClient Topic Subscription
Goto Forum:
  


Current Time: Thu Apr 18 00:16:13 GMT 2024

Powered by FUDForum. Page generated in 0.01898 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top