2

I am trying to connect to MQTT server manually and then subscribe to multiple topics. I am able to connect manually, but not able to connect multiple topics with same MQTT service.
Library used ngx-mqtt for Angular

Problem/Issue :- When try to connect to second Topic, Second topic will append to first Topic.

See pic below which is not working and my MQTT server is rejecting my call as 2 Topics are appended in one request for 2nd Topic, this has been tried multiple ways in code given below.

enter image description here

See pic below which is working on another old code.

enter image description here

Very poor documentation of ngx-mqtt leads to ask question for MQTT.

Tried couple of things in my code below are 2 of them i.e.

Create one observable as per documentation and subscribe

Created one Observable after adding both topics to it and then subscribe to it

  mqttServiceOpts1: IMqttServiceOptions = {
    connectOnCreate: false,
    hostname: 'example-mqtt.ca',
    port: 8090,
    path: '/mqtt',
    protocol: 'wss'
  }
  connetMqtt() {
    
    return new Promise((resolve, reject) => {
    this.getmqttDetails()
        .subscribe((data) => {
         
            console.log(data.port)
            console.log(data.clientId)
            console.log(data.broker)
            this.mqttServiceOpts1.clientId = data.clientId
            this.mqttService.connect(this.mqttServiceOpts1);

            const responsePustatusName= data.rootTopic + '/pustatus/inbox/+/response';
            const vacateRooutetopicName= data.rootTopic + '/vacateroute/wc/+/route/response';
            console.log('Response Topic Name ' + responsePustatusName);

           this.obs1$ = this.mqttService.observe(responsePustatusName)
           this.obs1$ = this.mqttService.observe(vacateRooutetopicName)
            this.subs2$ = this.obs1$.subscribe((message: IMqttMessage) => {
              console.log('msg: ', message.payload.toString())
              });
              
          })
          resolve(true)
        })
    }

Created 2 different observables

 mqttServiceOpts1: IMqttServiceOptions = {
    connectOnCreate: false,
    hostname: 'exmp-mqtt.ca',
    port: 8090,
    path: '/mqtt',
    protocol: 'wss'
  }
  connetMqtt() {
    
    return new Promise((resolve, reject) => {
    this.getmqttDetails()
        .subscribe((data) => {
         
            console.log(data.port)
            console.log(data.clientId)
            console.log(data.broker)
            this.mqttServiceOpts1.clientId = data.clientId
            this.mqttService.connect(this.mqttServiceOpts1);

            const responsePustatusName= data.rootTopic + '/pustatus/inbox/+/response';
            const vacateRooutetopicName= data.rootTopic + '/vacateroute/wc/+/route/response';
            console.log('Response Topic Name ' + responsePustatusName);

           this.subs1$ = this.mqttService.observe(responsePustatusName).subscribe((message: IMqttMessage) => {
              console.log('msg: ', message)
            });
            this.subs2$ = this.mqttService.observe(vacateRooutetopicName).subscribe((message: IMqttMessage) => {
              console.log('msg: ', message)
              });
              
          })
          resolve(true)
        })
    }
Inder R Singh
  • 652
  • 1
  • 6
  • 21

0 Answers0