Crate minimq

Source
Expand description

§MiniMQ

Provides a minimal MQTTv5 client and message parsing for the MQTT version 5 protocol.

This crate provides a minimalistic MQTT 5 client that can be used to publish topics to an MQTT broker and subscribe to receive messages on specific topics.

§Limitations

This library does not currently support the following elements:

  • Subscribing above Quality-of-service AtMostOnce
  • Server Authentication
  • Topic aliases

§Requirements

This library requires that the user provide it an object that implements a basic TcpStack that can be used as the transport layer for MQTT communications.

The maximum message size is configured through generic parameters. This allows the maximum message size to be configured by the user. Note that buffers will be allocated on the stack, so it is important to select a size such that the stack does not overflow.

§Example

Below is a sample snippet showing how this library is used.

use minimq::{ConfigBuilder, Minimq, Publication};

// Construct an MQTT client with a maximum packet size of 256 bytes
// and a maximum of 16 messages that are allowed to be "in flight".
// Messages are "in flight" if QoS::AtLeastOnce has not yet been acknowledged (PUBACK)
// or QoS::ExactlyOnce has not been completed (PUBCOMP).
// Connect to a broker at localhost - Use a client ID of "test".
let mut buffer = [0; 256];
let localhost: std::net::IpAddr = "127.0.0.1".parse().unwrap();
let mut mqtt: Minimq<'_, _, _, minimq::broker::IpBroker> = Minimq::new(
        std_embedded_nal::Stack::default(),
        std_embedded_time::StandardClock::default(),
        ConfigBuilder::new(localhost.into(), &mut buffer)
            .client_id("test").unwrap(),
        );

let mut subscribed = false;

loop {
    if mqtt.client().is_connected() && !subscribed {
        mqtt.client().subscribe(&["topic".into()], &[]).unwrap();
        subscribed = true;
    }

    // The client must be continually polled to update the MQTT state machine.
    mqtt.poll(|client, topic, message, properties| {
        match topic {
            "topic" => {
               println!("{:?}", message);
               client.publish(Publication::new("echo", message)).unwrap();
            },
            topic => println!("Unknown topic: {}", topic),
        };
    }).unwrap();
}

Re-exports§

pub use broker::Broker;
pub use config::ConfigBuilder;
pub use publication::Publication;
pub use mqtt_client::Minimq;
pub use embedded_nal;
pub use embedded_time;

Modules§

broker
config
mqtt_client
publication
types
MQTT-Specific Data Types

Structs§

Will

Enums§

DeError
Error
Possible errors encountered during an MQTT connection.
MinimqError
Property
All of the possible properties that MQTT version 5 supports.
ProtocolError
Errors that are specific to the MQTT protocol implementation.
PubError
QoS
The quality-of-service for an MQTT message.
ReasonCode
MQTTv5-defined codes that may be returned in response to control packets.
Retain
The retained status for an MQTT message.
SerError
Errors that result from the serialization process

Constants§

MQTT_INSECURE_DEFAULT_PORT
Default port number for unencrypted MQTT traffic
MQTT_SECURE_DEFAULT_PORT
Default port number for encrypted MQTT traffic