#mqtt #rpc-service #iot #rpc #response-pattern

mqtt-service

This crates provide a convenient support for the MQTT Response-Pattern. It uses the rumqttc library to connect to the MQTT broker.

7 releases (breaking)

0.7.0 Sep 15, 2025
0.6.0 Aug 25, 2025
0.5.0 Oct 2, 2024
0.4.0 Sep 25, 2024
0.1.0 Jun 22, 2024

#1731 in Network programming

Download history 7/week @ 2025-06-25 6/week @ 2025-07-02 18/week @ 2025-07-09 5/week @ 2025-07-16 15/week @ 2025-07-23 15/week @ 2025-07-30 10/week @ 2025-08-06 1/week @ 2025-08-13 152/week @ 2025-08-20 55/week @ 2025-08-27 32/week @ 2025-09-03 168/week @ 2025-09-10 98/week @ 2025-09-17 37/week @ 2025-09-24 68/week @ 2025-10-01 19/week @ 2025-10-08

277 downloads per month
Used in 2 crates

MIT license

22KB
495 lines

stable pipeline dev/1 pipeline docs crates.io

mqtt-service

This crate provides a convenient support for the MQTT Response-Pattern. It uses the rumqttc library to connect to the MQTT broker.

Note: this crate is feature complete, and few changes are expected.

How to use

Add mqtt-service, mqtt-client and tokio to your project:

cargo add mqtt-service

mqtt-service needs a tokio runtime and a mqtt_channel::Client:

let rt = tokio::runtime::Runtime::new().unwrap();
let (connection, task) = mqtt_channel::Client::build(
  "name-of-the-client-raw",
  env::var("MQTT_SERVICE_MQTT_SERVER_HOSTNAME").unwrap_or("localhost".to_string()),
  1883,
)
.start();
rt.spawn(task);

Then a mqtt-service client can be created:

let client = Client::new(connection);

Then you can create a JSON service with:

rt.spawn(
  connection
    .clone()
    .create_json_service(
      "mqtt-service/test_json/addition",
      Box::new(|request: &Request| Response {
        r: request.a + request.b,
      }),
      10,
    )
    .map(|r| r.unwrap()),
);

Alternatively, you can use create_raw_service for higher control on the serialization of message. Then the service can be called using call_json_service (or call_raw_service):

let fut = connection
  .call_json_service::<Request, Response>(
    "mqtt-service/test_json/addition",
    &Request { a: 1.0, b: 2.0 },
  )
  .unwrap();

let res = rt.block_on(fut).unwrap();

Compatibility

rumqttc mqtt-channel
0.25 0.7
0.24 0.1-0.6

Dependencies

~7–19MB
~205K SLoC