Skip to content

clecat/ocaml-mqtt

 
 

Repository files navigation

OCaml MQTT Client

OCaml-CI Build Status

This library implements the client MQTT v3 protocol.

Originally forked from https://github.com/j0sh/ocaml-mqtt.

Quickstart

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)))

Examples

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();
};

Packages

No packages published

Languages

  • OCaml 76.8%
  • CSS 22.0%
  • Other 1.2%