Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dcs3spp opened this issue May 27, 2020 · 3 comments

Comments

@dcs3spp
Copy link

dcs3spp commented May 27, 2020

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?

@RangerMauve
Copy link
Contributor

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
Copy link

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

@RangerMauve
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants