Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

393
Views
Reconnection in MQTT using javascript

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();
   
    }
  }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!