Expand description
A small blocking MQTT client on top of stem-mqtt-proto.
No async runtime, no TLS stack of its own: it drives the codec over any Transport (a
std::net::TcpStream on a PC or on an ESP32 with ESP-IDF std, a TLS stream, a test double).
The caller owns the loop: call Client::poll regularly, it receives messages and keeps the
connection alive (PINGREQ) by itself.
use std::net::TcpStream;
use std::time::Duration;
use mqtt_client_blocking::{Client, Options, QoS};
let stream = TcpStream::connect("192.168.1.10:1883")?;
let mut client = Client::connect(stream, Options::new("weather-c6"))?;
client.subscribe("home/+/command", QoS::AtLeastOnce)?;
client.publish("home/c6/status", b"online", QoS::AtLeastOnce, true)?;
loop {
if let Some(msg) = client.poll(Duration::from_millis(500))? {
println!("{}: {}", msg.topic, String::from_utf8_lossy(&msg.payload));
}
}Scope: QoS 0 and 1 for publishing, QoS 0, 1 and 2 for receiving (a QoS 2 message is delivered
once, when the PUBLISH arrives, and completed with PUBREC/PUBCOMP). Publishing at QoS 2 is
refused with Error::Unsupported. Reconnecting is the caller’s job: after an error, drop the
client and connect again.
Structs§
- Client
- A connected client.
- Message
- A message received from the broker.
- Options
- Connection settings.
- Stats
- Traffic counters (for a status screen).
- Will
Message - A message the client sends if the connection is lost without a DISCONNECT.
Enums§
- Error
- What went wrong.
- Mqtt
Version - The MQTT protocol version negotiated for a connection.
- QoS
- MQTT Quality of Service level.
Constants§
- DEFAULT_
ACK_ TIMEOUT - How long
Client::connect,Client::publishat QoS 1 andClient::subscribewait for the broker’s answer.
Traits§
- Transport
- A byte stream with a read timeout, so that
Client::pollcan wait a bounded time.