mqttjs / async-mqtt

Promise wrapper over MQTT.js
MIT License
272 stars 50 forks source link

connectAsync blocking, promise not rejecting when mqtt server is not running #35

Open dcs3spp opened 4 years ago

dcs3spp commented 4 years ago

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:

    /**
     * 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?

RangerMauve commented 4 years ago

Oo, that's a tricky one. Usually what I do in situations like that is do a Promise.race which throws an error after a delay to timeout a connection.

I'd be down to review a PR that adds a maxRetries field or something similar if you're interested in taking a crack at it.

shivamydv commented 3 years ago

Is there any update on this? Will this feature we implemented in the future versions?

RangerMauve commented 3 years ago

@shivamydv I'm still down to review/merge a PR if you'd like to take a shot at implementing this functionality. 😁