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: embedded_nal::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);
               let response = Publication::new(message).topic("echo").finish().unwrap();
               client.publish(response).unwrap();
            },
            topic => println!("Unknown topic: {}", topic),
        };
    }).unwrap();
}

Re-exports

Modules

Structs

Enums

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

Constants