0

I've managed to connect to my Mosquitto broker of Paho MQTT JS client. However, the client disconnects inmediately after completing the connection. Before it worked now I'm testing it doesn't work.

This is my code for the main functions involved: constructor() {

    super();
    this.onConnectionLost = this.onConnectionLost.bind(this)
    this.onConnect = this.onConnect.bind(this)
    this.onMessageArrived = this.onMessageArrived.bind(this)

    const client = new Paho.MQTT.Client(process.REACT_APP_HOST, Number(process.REACT_APP_PORT), clientID);
    client.onConnectionLost = this.onConnectionLost;
    client.onConnect = this.onConnect;
    client.onMessageArrived = this.onMessageArrived

constuctor(){
    client.connect({

      onSuccess: this.onConnect,
      userName: process.REACT_APP_DB_NAME,
      password: process.REACT_APP_PASSWORD,
      onFailure: this.onConnectionLost,

    });
    this.state = {

      client,
      value: 0,
    }

  }

  componentDidMount() {

    this.onConnect = this.onConnect.bind(this);

  }

  onConnect = () => {
    const { client } = this.state;
    console.log("Connected!!!!");

  }

  onConnectionLost = (responseObject) => {
    if (responseObject.errorCode !== 0) {
      console.log("onConnectionLost : " + responseObject.errorMessage);
    }
  }

  sendIntensity = () => {

    const { client } = this.state;

   client.publish(this.props.topic, this.state.value.toString(), 1)

  }

console log:

Connected!!!!
Connected!!!!
onConnectionLost : AMQJS0007E Socket error:undefined.
onConnectionLost : AMQJS0007E Socket error:undefined.
Connected!!!!

Error :

Error: AMQJS0011E Invalid state not connected. at node_modules\react-native\Libraries\LogBox....

6
  • 2
    Are you trying to connect multiple times with the same client ID? Some brokers will drop a connection when a client ID is duplicated. Commented Apr 26, 2022 at 15:45
  • no i'm using random id : const clientID = "clientId-" + parseInt(Math.random() * 100, 10)
    – Kindth
    Commented Apr 26, 2022 at 15:47
  • 1
    What do your mosquitto logs show? Also your console log is showing "Connected!!!" twice without any disconnect between them... is your code connecting twice before the disconnect?
    – romkey
    Commented Apr 26, 2022 at 16:04
  • 1
    Hence my question on whether you are somehow trying to connect multiple times with the same client ID. Perhaps you can log the clientID in OnConnect and OnConnectionLost events. Commented Apr 26, 2022 at 16:29
  • @romkey yeah it connect twice without disconnect every run I don't know why. Have you an idea ab it?
    – Kindth
    Commented Apr 27, 2022 at 15:13

1 Answer 1

1
 const client = new Paho.MQTT.Client(process.REACT_APP_HOST, Number(process.REACT_APP_PORT), clientID);
    client.onConnectionLost = this.onConnectionLost;
    client.onMessageArrived = this.onMessageArrived

constuctor(){
    client.connect({

      onSuccess: this.onConnect,
      userName: process.REACT_APP_DB_NAME,
      password: process.REACT_APP_PASSWORD,
      onFailure: this.onConnectionLost,

    });

You seem to define the handle to client.onConnect twice, which might account for connecting twice with the same client instance, hence the same client id and angry result.

2
  • So I shouldn't bind Connect func this.onConnect = this.onConnect.bind(this) ??
    – Kindth
    Commented Jul 4, 2022 at 10:36
  • 1
    calling the onConnect() function more than once is redundantly redundant. I amended the answer to be more clear as a solution vs. "remove this line" The instantiation of the client object carries some event definitions onConnectionLost and onMessageArrived. You have handled them already. I must also admit that I don't really follow the syntax as written in your example. It may be my own ignorance showing. Perhaps it would be a wise to add a lot of console.log("this is where I am"); statements so you can trace it in the browser JS console and see how it gets called more than once Commented Jul 5, 2022 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.