Description
Hi,
The following code below permanently blocks when using connectAsync and the MQTT server is not running.
The catch handler is not being triggered. How do I correctly use connectAsync
so that after a connect timeout period an error is caught and notified to the user??
I am calling connectAsync with the following arguments:
- url: mqtts://user:pass@myhost.local
- options: {
ca: /path/to/ca/file
rejectUnauthorised: true
}
/**
* Publish a message to the mqtt broker for a given topic
* Disposes the client connection upon send success or failure.
* @param {string} topic - topic to publish to
* @param {Object} message - message to publish
*/
async publish(topic, message) {
let client = undefined;
try {
console.log('MQTTClient awaiting connection to broker for publishing...');
client = await mqtt.connectAsync(this.#url, this.#options);
console.log('MQTTClient acquired connection, publishing...');
await client.publish(topic, message, this.#options);
console.log(`Published message to topic := ${topic}`);
} catch(e) {
console.error(`Failed to publish message to topic := ${topic}`);
console.error(`message := ${e.message}`);
console.error(`stack := ${e.stack}`);
}
finally {
console.log('MQTTClient closing connection');
if(client)
await client.end();
}
}
Edit: Ahhh just looked at the source code. If I include an additional argument allowRetries
to false then the catch handler triggers. So, if the allowRetries
argument is not included then the default behaviour is that the client will continuously try to connect and code will block permanently until it is reconnected? Is my understanding correct?
Is there any option to specify the number of retries before a connection is deemed to have failed?