user18774: Are there any MQTT libraries that do not block while connecting?
I'm currently using PubSubClient, and the connection part contains this code:
https://github.com/knolleary/pubsubclient/blob/master/src/PubSubClient.cpp

        while (!_client->available()) {
            unsigned long t = millis();
            if (t-lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) {
                _state = MQTT_CONNECTION_TIMEOUT;
                _client->stop();
                return false;
            }
        }

I'd like my main loop to continue, or a callback to be called, while the MQTT connection is established, rather than waiting for ages if the MQTT server is unavailable.
Update:
I wrote a modification to take a callback function, and called it in an else clause added to that if statement, and discovered that the blocking delay is not this while loop. I think it's in the _client->connect at the start of the method.

user85299: I had the same question and found an mqtt library that is completely non-blocking.

https://github.com/marvinroger/async-mqtt-client

You need to manually install it and the docs give instructions:

https://github.com/marvinroger/async-mqtt-client/blob/develop/docs/1.-Getting-started.md

It requires one additional library:

for esp8266: https://github.com/me-no-dev/ESPAsyncTCP
for esp32:   https://github.com/me-no-dev/AsyncTCP

I also liked the example's use of the Ticker library as a general tool for non-blocking code.

18774: <p>Are there any MQTT libraries that do not block while connecting?</p>

<p>I'm currently using PubSubClient, and the connection part contains this code:
<a href="https://github.com/knolleary/pubsubclient/blob/master/src/PubSubClient.cpp" rel="nofollow noreferrer">https://github.com/knolleary/pubsubclient/blob/master/src/PubSubClient.cpp</a>
</p>

<pre class="lang-cpp prettyprint-override"><code>        while (!_client-&gt;available()) {
            unsigned long t = millis();
            if (t-lastInActivity &gt;= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) {
                _state = MQTT_CONNECTION_TIMEOUT;
                _client-&gt;stop();
                return false;
            }
        }
</code></pre>

<p>I'd like my main loop to continue, or a callback to be called, while the MQTT connection is established, rather than waiting for ages if the MQTT server is unavailable.</p>

<p><strong>Update:</strong></p>

<p>I wrote a modification to take a callback function, and called it in an else clause added to that if statement, and discovered that the blocking delay is not this while loop. I think it's in the _client->connect at the start of the method.</p>
 85299: <p>I had the same question and found an mqtt library that is completely non-blocking.</p>
<ul>
<li><a href="https://github.com/marvinroger/async-mqtt-client" rel="nofollow noreferrer">https://github.com/marvinroger/async-mqtt-client</a></li>
</ul>
<p>You need to manually install it and the docs give instructions:</p>
<ul>
<li><a href="https://github.com/marvinroger/async-mqtt-client/blob/develop/docs/1.-Getting-started.md" rel="nofollow noreferrer">https://github.com/marvinroger/async-mqtt-client/blob/develop/docs/1.-Getting-started.md</a></li>
</ul>
<p>It requires one additional library:</p>
<ul>
<li>for esp8266: <a href="https://github.com/me-no-dev/ESPAsyncTCP" rel="nofollow noreferrer">https://github.com/me-no-dev/ESPAsyncTCP</a></li>
<li>for esp32:   <a href="https://github.com/me-no-dev/AsyncTCP" rel="nofollow noreferrer">https://github.com/me-no-dev/AsyncTCP</a></li>
</ul>
<p>I also liked the example's use of the Ticker library as a general tool for non-blocking code.</p>
