Expand description
This crates provides a basic MQTT client implementation with a high-level, asynchronous interface.
Connecting
Connecting to a broker can be achieved using the Client::connect() function, by passing a
hostname (or IP address) together with Options:
Publishing messages
The basic ways to publish messages are:
Client::publish_qos_0()to publish a message with a Quality of Service of “AtMostOnce”. Unfortunately, there is no way to know if and when this message has reached the broker safely.Client::publish_qos_1()to publish a message with a Quality of Service of “AtLeastOnce”. By settingawait_ackto true, it is possible to wait for the acknowledgment packet and make sure that the broker has received the message.Client::publish_qos_2()to publish a message with a Quality of Service of “ExactlyOnce”. By specifying a value other thanPublishEvent::Nonetoawait_event, it is possible to wait for the acknowledgment packet and make sure that the broker has received the message.
If it is not necessary to wait for the publish packet to be sent, Client::publish_no_wait() can also be used.
Finally, Client::try_publish() also be used to attempt to publish a packet with no need for any await.
Subscribing to topics
There are multiple ways to subscribe to topics, each of them have their own limitations:
Client::subscribe_lossy()will create a subscription based on a bounded queue. If the queue is full when the message is received, it will not be pushed onto that queue and may be lost.Client::subscribe_unbounded()will create a subscription based on an unbounded queue, bypassing the limitations of a bounded subscription at a slightly increased performance cost.Client::subscribe_fast_callback()will cause the passed function to be called every time a message is received. However, the function must be thread-safe and cannot block.
There is also Client::subscribe_hold(), which cannot be used to read messages but can be used to prevent
the client from unsubscribing from a topic automatically.
Note that LMC subscriptions and MQTT subscriptions are not the same thing. LMC subscriptions are created using
any of the subscribe methods. MQTT subscriptions are created by the implementation, as a result of the creation
of an LMC subscription to a new topic. If an LMC subscription exists for a given topic, that means that an MQTT
subscription already exists and there is thus no need to create a new one. This does mean that only the first
LMC subscription will receive the retained messages of a particular topic (if there are any). So, if the first
subscription is a “hold” subscription, retained messages will be lost.
MQTT subscriptions will only be cancelled (unsubscribed) if there are no more valid LMC subscription for that topic,
or if Client::unsubscribe() is called directly. Note that automatic unsubscribe can only be triggered by removing
a fast callback subscription or by an incoming message in that topic. This is because the transceiver task does not
actively check if subscription queues are closed.
Re-exports
Modules
Structs
ShutdownStatus.Arc, so this value can be safely cloned with no extra
mallocs.Enums
Client’s publish functions.QoS::ExactlyOnce
that can be awaited on.CONNECT packet.Client’s subscribe functions.Client::get_subscription_status().Client::try_publish().