arcoirislabs / cordova-plugin-mqtt

MqTT Cordova Plugin for Apache Cordova (> v3.0)
MIT License
86 stars 49 forks source link

MQTT serverConnection lost #44

Open vamshik113 opened 5 years ago

vamshik113 commented 5 years ago

MQTT plugin suddenly disconnects and when tried to reconnect back it shows the error: Error while connecting to MQTT serverConnection lost (32109) - java.net.SocketException: Software caused connection abort. This happens very often. Is there any fix for this?

NiragShah commented 5 years ago

Hi There, I am also getting same issue in app. When i connect app with sensors and publish data, after 2-3 mins app suddenly drops connection to MQTT broker with following error. "Error while connecting to MQTT serverConnection lost (32109) - java.net.SocketException: Connection timed out" Any help would be appreciated. Thanks

adrianmihaila commented 5 years ago

I can help you if you need it...

vamshik113 commented 5 years ago

@adrianmihaila Yes, I'm still facing this issue. Please let me know what is the fix.

adrianmihaila commented 5 years ago

@vamshik113, I had the same problem, but I found a workaround.

class MqttClient {
  connect(successCallback) {
    let clientId = 'm-' + device.uuid;

    cordova.plugins.CordovaMqTTPlugin.connect({
      url: MQTT_BROKER_URL,
      port: MQTT_BROKER_PORT,
      clientId: clientId,
      keepAlive: 0,
      reconnectPeriod: 1000,
      clean: false,
      success: successCallback,
      error: (e) => {
        console.error('Mqtt connection error: ' + JSON.stringify(e));
      },
      onConnectionLost: () => {
        this.connect(successCallback); // Here is the magic. On connection lost, you will try to reconnect.
      }
    });
  }
....
}

document.addEventListener('deviceready', () => {
    let mqttClient = new MqttClient();
    mqttClient.connect(() => {
          // Subscribe method is another wrapper for cordova.plugins.CordovaMqTTPlugin.subscribe()
          mqttClient.subscribe(TOPIC, QOS_LEVEL);
    });
});

I made a suite of manual tests and all of them have passed.

Adi

spilz87 commented 3 years ago

Hi

I got the same issue when two tablets with the same application connect to MQTT Server (Mosquitto on raspberry pi)

I observe that the application lost connection with server (Error while connecting to MQTT serverConnection lost (32109) - java.net.SocketException: Software caused connection abort) when on an other tablet the application connect.

They should have different Client ID cordova.plugins.CordovaMqTTPlugin.connect({ url:"tcp://" + config.gateway, port:config.port, clientId:"TABLETTE_" + macAdd, connectionTimeout:3000, willTopicConfig:{qos:0, //default is 0 retain:true, //default is true topic: lastWillTopic, payload: lastWillPayload }, username:config.MQTT_log, password:config.MQTT_PW, keepAlive:10, isBinaryPayload: false });

but I suspect an issue about that and the ClientID is the same for all application (whatever you set as clientId)

any idea ?

arcoirislabs commented 2 years ago

Hi, According to MQTT spec, we cannot reuse same Client ID which is used previously. The broker shall deny connection or close previous connection. The library generates a random Client ID if it is unset or left blank.

spilz87 commented 2 years ago

Actually if clientId is not set broker receive id null

It seems the library doesn't generate random clientId

arcoirislabs commented 2 years ago

A newer version is being worked on here https://github.com/ameykshirsagar/cordova-plugin-mqtt . You can keep a tab on developments