linux – How to make Paho MQTT C++ Client Library wait for initial connection?
I’m using Paho MQTT C++ Client Library on Ubuntu 22.04 and 24.04 to publish messages to remote MQTT broker. Here is the snippet:
#include "mqtt/client.h"
int main() {
mqtt::client client("192.168.0.19", "", nullptr);
mqtt::connect_options connOpts;
connOpts.set_keep_alive_interval(3600);
connOpts.set_connect_timeout(5);
connOpts.set_clean_session(true);
connOpts.set_automatic_reconnect(true);
try {
client.connect(connOpts);
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
}
My test case tries to connect to inactive broker at 192.168.0.19. Despite connect timeout set to 5s and automatic reconnect activated, the program fails immediately with MQTT error [-1]: TCP/TLS connect failure output. It behaves similarily with mqtt::async_client. Is there a way to make it retry for set timeout, i.e. 5s, before failing?
Read more here: Source link
