This section guides you through the steps to properly configure the to establish
a connection with your chosen MQTT Broker.
mqtt_client
The
does not expose connect functions (nor asynchronous
connect functions). Instead, it features a function that will start the Client
(see mqtt_clientmqtt_client::async_run).
All configuration of the must be finalised
before invoking mqtt_clientmqtt_client::async_run.
Upon calling this function, the makes its initial
attempt to connect.
mqtt_client
The initial step is selecting an appropriate transport protocol for your MQTT connection.
The MQTT protocol relies on an underlying transport protocol that guarantees the orderly and reliable transmission of byte streams between the Client and Server in both directions. Common transport protocols meeting these criteria include TCP/IP, TLS/SSL for secure transmissions, and WebSocket over TCP/IP and TLS/SSL.
MQTT Brokers come in various implementations and support different transport protocols. Therefore, it is important to familiarise yourself with the protocols your chosen Broker supports. Additionally, gather any necessary details such as certificate authority, client certificate, or private keys, which might be required for establishing a secure connection.
The upcoming examples will demonstrate the configuration and the publishing
of a "Hello World!" application message at QoS
0, using various transport protocols:
The
offers a variety of functions for customising your MQTT connection settings.
These functionalities include:
mqtt_client
mqtt_client will try
to connect to (see mqtt_client::brokers).
The mqtt_client allows
for the specification of multiple Brokers for the connections. It is
important to ensure that only Brokers belonging
to the same cluster are included in this list. Listing Brokers
from different clusters may lead to inconsistencies between MQTT Sessions.
mqtt_client::credentials).
mqtt_client::keep_alive).
Enhanced Authentication
(mqtt_client::authenticator).
CONNECT packet sent during
connection initiation (see mqtt_client::connect_property
and mqtt_client::connect_properties).
It is important to note that these configurations apply to every Broker listed
in mqtt_client::brokers.
To modify any configuration parameters, you must first stop the using mqtt_clientmqtt_client::cancel
or mqtt_client::async_disconnect.
Afterwards, you can re-apply the configurations and restart the with mqtt_clientmqtt_client::async_run.
The
initiates a connection with the first Broker from the list of Brokers assigned
using mqtt_clientmqtt_client::brokers.
A single attempt at connecting involves several steps:
CONNECT packet to the Broker,
(if applicable) exchanging AUTH packets (see Enhanced Authentication),
and continuing until the Broker sends the CONNACK packet.
The connection is successfully established once the receives a
mqtt_clientCONNACK packet with Reason Code
0x00 (Success).
The attempt fails if any of the following occurs:
mqtt_client does not
receive the results of the hostname resolution within 5
seconds.
mqtt_client is unable
to complete steps 2 through 5 within 5
seconds.
If this connection attempt is unsuccessful, it proceeds to the next Broker
in order, continuing this process until it establishes a connection or exhausts
all options without success. Upon reaching the end of the list without a
successful connection, the enters a backoff
period before making another round of attempts with the entire list. The
duration of this backoff period in milliseconds is determined by the formula:
mqtt_client
2^(min(retry_count, 4)) * 1000 + jitter
In this formula, retry_count
represents the number of complete cycles through the list without a successful
connection, and jitter is
a randomly chosen value between -500ms to
500ms,
intended to prevent synchronised reconnection attempts in scenarios with
multiple clients.