I'm trying to reconnect in MQTT. I have many pages every page received data from different sensor. I tried to use reconnect: true and keepaliveIInterval: 30, but thats made my app crashed after few minutes and it doesnt let me the navigate to another page so I tried another solution which is to invoked the connect function after every lost of conn (because when I navigate to another page the connection is lost) but that's not working. It returns the last data after I swich to another page so it doesn't resend subscribe. Any help is appreciated.
constructor(){
super();
this.onMessageArrived = this.onMessageArrived.bind(this)
this.onConnectionLost = this.onConnectionLost.bind(this)
this.onConnect = this.onConnect.bind(this)
this.onFailure = this.onFailure.bind(this)
const client = new Paho.MQTT.Client(host, Number(port), clientID);
client.onMessageArrived = this.onMessageArrived;
client.onFailure = this.onFailure;
client.MQTTconnect = this.MQTTconnect;
client.connect({
onSuccess : this.onConnect,
userName: "admin",
password: "admin",
onFailure : this.onFailure,
});
componentDidMount() {
this.onConnect = this.onConnect.bind(this);
}
onFailure(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("Connection Attempt to host"+ host +" Failed");
}
}
MQTTconnect() {
const client = new Paho.MQTT.Client(host, Number(port), clientID);
client.onMessageArrived = this.onMessageArrived;
client.connect({
onSuccess : this.onConnect,
userName: "admin",
password: "admin",
onFailure : this.onFailure,
});
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost : "+responseObject.errorMessage);
this.setState({error: 'Lost Connection', isConnected: false});
this.MQTTconnect();
}
}