Home > Software engineering >  Reconnection failed : paho mqtt using javascript
Reconnection failed : paho mqtt using javascript

Time:12-06

When the connection is lost, the function onConnectionLost will be called, but how do I reconnect when the connection is lost?

here is my code :

     const client = new Paho.MQTT.Client(host, Number(port), clientID);
    client.onMessageArrived = this.onMessageArrived;
    client.onConnectionLost = this.onConnectionLost;
    client.connect({ 
           cleanSession : false, 
                onSuccess : this.onConnect, 
                userName: "user",
                password: "pass",
                onFailure : this.onConnectionLost, 
                keepAliveInterval: 30, 
                reconnect : true,         // Enable automatic reconnect
                reconnectInterval: 10     // Reconnect attempt interval : 10 seconds
    });
onConnect = () => {
    const { client } = this.state;
    console.log("Connected!!!!");
    this.setState({isConnected: true, error: ''});
    client.subscribe(topic, qos=1);
    client.subscribe(topic1, qos=1);
    client.subscribe(topic2, qos=1);
  };

  onConnectionLost(responseObject) {
    if (responseObject.errorCode !== 0) {
      console.log("onConnectionLost : " responseObject.errorMessage);
      this.setState({error: 'Lost Connection', isConnected: false});
    }
  }

Error: Unknown property, reconnectInterval. Valid properties are: timeout userName password willMessage keepAliveInterval cleanSession useSSL invocationContext onSuccess onFailure hosts ports reconnect mqttVersion mqttVersionExplicit uris

CodePudding user response:

Use the reconnect parameter

client.connect(
            {
                cleanSession : false, 
                onSuccess : onConnectSuccess, 
                onFailure : onFailedConnect, 
                keepAliveInterval: 30, 
                reconnect : true,         // Enable automatic reconnect
            }

https://www.eclipse.org/paho/files/jsdoc/Paho.MQTT.Client.html

  • Related