Issues publishing data to an MQTT server (HiveMQ) in a workflow

This topic was automatically generated from Slack. You can find the original thread here.

Gio : ```
Hello everyone, I would like to try pipedream to publish data on a mqtt server (HiveMQ) but I can’t. What am I doing wrong? Thanks

`var mqtt = require('mqtt')`
`var PORT    = 8883;`
`var HOST    = 'mqtts://7a****************************.[s1.eu](http://s1.eu).hivemq.cloud';`
`var TOPIC   = 'smart**************************************************************/ACCESO';`
`var topic_s = "message";`
`var options = {`
  `clientId:"mqttjs012345",`
  `//port: PORT,`
  `//protocol:'mqtts',`
  `host: HOST,`
  `username:"**********1234",`
  `password:"*********1234",`
  `//clean:true,`
  `rejectUnauthorized : false,`
`};`

`//Connessione:`
`console.log('Connessione in corso....');`
`var client  = mqtt.connect(HOST, options);`

`//Gestione errore:`
`client.on("error", function(error)`
`{`
  `console.log("Can't connect: " + error);`
  `process.exit(1);`
`})`

`//Azione su connessione:`
`client.on('connect', function ()` 
`{`
  `console.log("flag di connessione: " + client.connected);`
  `//client.subscribe(topic_s, function (err) {`
  `client.subscribe(TOPIC, function (err) {`
    `if (!err) {`
      `client.publish(TOPIC, 'True')`
    `}`
  `})`
`})`

`//Azione su messaggio:`
`client.on('message', function (topic, message)` 
`{`
  `console.log("Topic:" + TOPIC);`
  `console.log("Valore:" + message);`
  `client.end();`
`})`

Dylan Sather (Pipedream) : Hi Gio, take a look at this package and let me know if that sample code works: https://www.npmjs.com/package/async-mqtt . This wraps the mqtt package with Promise support, instead of using callbacks. You can read more about why that’s necessary here: https://pipedream.com/docs/workflows/steps/code/async/ .

Let me know if that helps.

Gio : ```
Thanks for the directions. Very useful. I tried this too but I can’t write new values ​​… I tried with this example:

`const mqtt = require('async-mqtt')`
`var PORT    = 8883;`
`var HOST2   = '7a7************[a.s1.eu](http://a.s1.eu).hivemq.cloud';`
`var TOPIC = "message";`
`var options = {`
  `clientId:"mqttjs012345",`
  `protocol:'mqtts',`
  `host: HOST2,`
  `port: PORT,`
  `username:"********1234",`
  `password:"********1234",`
  `//clean:true,`
  `rejectUnauthorized : false,`
`};`


`const client = mqtt.connect(options);`

`// When passing async functions as event listeners, make sure to have a try catch block`

`const doStuff = async () => {`
	`console.log("Starting...");`
	`try {`
		`await client.publish(TOPIC, "It works!");`
		`// This line doesn't run until the server responds to the publish`
		`await client.end();`
		`// This line doesn't run until the client has disconnected without error`
		`console.log("Done");`
	`} catch (e){`
		`// Do something about it!`
		`console.log(e.stack);`
		`process.exit();`
	`}`
`}`

`client.on("connect", doStuff);`

Gio : ```
The result is always this:
Connecting…
ACTIVE_HANDLEThis step was still trying to run code when the step ended. Make sure you promisify callback functions and await all Promises. (Reason: GetAddrInfoReqWrap, Learn more: Running asynchronous code)

Gio : ```
What is the best way to debug this code to understand what’s going on?

Dylan Sather (Pipedream) : Can you try the connectAsync version, and see if that works?

const MQTT = require("async-mqtt");
 

const client = await MQTT.connectAsync("tcp://somehost.com:1883")
 
console.log("Starting");
try {
  await client.publish("wow/so/cool", "It works!");
  await client.end();
} catch (e) {
  console.log(e.stack);
}

Gio : ```
Thanks Dylan, now it works great !!! Thank you so much I had been trying for days. Great, Thank you!!!

Dylan Sather (Pipedream) : Great to hear!