#mqtt #iot #coap #http


An efficient and robust mqtt client for your connected devices

29 releases (breaking)

0.20.0 Jan 17, 2023
0.19.0 Dec 28, 2022
0.18.0 Nov 21, 2022
0.13.0 Jun 16, 2022
0.0.5 Jul 26, 2020

#21 in Network programming

Download history 4863/week @ 2022-12-03 6376/week @ 2022-12-10 5150/week @ 2022-12-17 1850/week @ 2022-12-24 4772/week @ 2022-12-31 6491/week @ 2023-01-07 7284/week @ 2023-01-14 7570/week @ 2023-01-21 7583/week @ 2023-01-28 7336/week @ 2023-02-04 6113/week @ 2023-02-11 7003/week @ 2023-02-18 9110/week @ 2023-02-25 9117/week @ 2023-03-04 7672/week @ 2023-03-11 7532/week @ 2023-03-18

34,606 downloads per month
Used in 45 crates (34 directly)




crates.io page docs.rs page

A pure rust MQTT client which strives to be robust, efficient and easy to use. This library is backed by an async(using tokio) eventloop which enables users to send and receive MQTT messages in correspondence with a broker.


A simple synchronous publish and subscribe

use rumqttc::{MqttOptions, Client, QoS};
use std::time::Duration;
use std::thread;

let mut mqttoptions = MqttOptions::new("rumqtt-sync", "test.mosquitto.org", 1883);

let (mut client, mut connection) = Client::new(mqttoptions, 10);
client.subscribe("hello/rumqtt", QoS::AtMostOnce).unwrap();
thread::spawn(move || for i in 0..10 {
   client.publish("hello/rumqtt", QoS::AtLeastOnce, false, vec![i; i as usize]).unwrap();

// Iterate to poll the eventloop for connection progress
for (i, notification) in connection.iter().enumerate() {
    println!("Notification = {:?}", notification);

A simple asynchronous publish and subscribe

use rumqttc::{MqttOptions, AsyncClient, QoS};
use tokio::{task, time};
use std::time::Duration;
use std::error::Error;

let mut mqttoptions = MqttOptions::new("rumqtt-async", "test.mosquitto.org", 1883);

let (mut client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
client.subscribe("hello/rumqtt", QoS::AtMostOnce).await.unwrap();

task::spawn(async move {
    for i in 0..10 {
        client.publish("hello/rumqtt", QoS::AtLeastOnce, false, vec![i; i as usize]).await.unwrap();

while let Ok(notification) = eventloop.poll().await {
    println!("Received = {:?}", notification);

Quick overview of features

  • Eventloop orchestrates outgoing/incoming packets concurrently and handles the state
  • Pings the broker when necessary and detects client side half open connections as well
  • Throttling of outgoing packets (todo)
  • Queue size based flow control on outgoing packets
  • Automatic reconnections by just continuing the eventloop.poll()/connection.iter() loop
  • Natural backpressure to client APIs during bad network
  • Support for WebSockets
  • Secure transport using TLS

In short, everything necessary to maintain a robust connection

Since the eventloop is externally polled (with iter()/poll() in a loop) out side the library and Eventloop is accessible, users can

  • Distribute incoming messages based on topics
  • Stop it when required
  • Access internal state for use cases like graceful shutdown or to modify options before reconnection

Important notes

  • Looping on connection.iter()/eventloop.poll() is necessary to run the event loop and make progress. It yields incoming and outgoing activity notifications which allows customization as you see fit.

  • Blocking inside the connection.iter()/eventloop.poll() loop will block connection progress.


Connecting to a broker using raw ip doesn't work

You cannot create a TLS connection to a bare IP address with a self-signed certificate. This is a limitation of rustls. One workaround, which only works on certain systems, is to add an entry to wherever your DNS resolver looks (e.g. /etc/hosts) for the bare IP address and use that name in your code.


~244K SLoC