This library implements the client MQTT v3 protocol.
Originally forked from https://github.com/j0sh/ocaml-mqtt.
To install ocaml-mqtt in an esy project, add the following dependency to your package.json file:
"dependencies": {
"ocaml-mqtt": "odis-labs/ocaml-mqtt#3a97e01"
}
In your dune project add the following dependencies to your dune file:
(executable
(name My_app)
(public_name my-app)
(libraries mqtt-client lwt)
(preprocess (pps lwt_ppx)))
Here is a basic example of a subscriber:
open Printf;
let sub_example = () => {
let%lwt client = Mqtt_client.connect(~id="client-1", ~port, [host]);
let%lwt () = Mqtt_client.subscribe([("topic-1", Mqtt_client.Atmost_once)], client);
let stream = Mqtt_client.messages(client);
let rec loop = () => {
let%lwt msg = Lwt_stream.get(stream);
switch(msg) {
| None => Lwt_io.printl("STREAM FINISHED")
| Some((topic, payload)) =>
printlf("%s: %s", topic, payload);
loop()
};
};
loop();
};