Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
rizo authored Mar 20, 2024
1 parent a8e5c56 commit 59ca674
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,16 @@ This library implements the client MQTT v3 protocol.
* [API Documentation](https://hyper-systems.github.io/ocaml-mqtt/mqtt-client/Mqtt_client)
* [Issues](https://github.com/hyper-systems/ocaml-mqtt/issues)

Originally forked from https://github.com/j0sh/ocaml-mqtt.
> 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:

```json
"dependencies": {
"ocaml-mqtt": "odis-labs/ocaml-mqtt#3a97e01"
}
```

In your dune project add the following dependencies to your dune file:


```lisp
(executable
(name My_app)
(public_name my-app)
(public_name my_app)
(libraries mqtt-client lwt)
(preprocess (pps lwt_ppx)))
```
Expand All @@ -35,21 +26,28 @@ In your dune project add the following dependencies to your dune file:
Here is a basic example of a subscriber:

```reason
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();
};
module C = Mqtt_client
let host = "127.0.0.1"
let port = 1883
let sub_example () =
let on_message ~topic payload = Lwt_io.printlf "%s: %s" topic payload in
let%lwt () = Lwt_io.printl "Starting subscriber..." in
let%lwt client = C.connect ~on_message ~id:"client-1" ~port [ host ] in
C.subscribe [ ("topic-1", C.Atmost_once) ] client
let pub_example () =
let%lwt () = Lwt_io.printl "Starting publisher..." in
let%lwt client = C.connect ~id:"client-1" ~port [ host ] in
let rec loop () =
let%lwt () = Lwt_io.printl "Publishing..." in
let%lwt line = Lwt_io.read_line Lwt_io.stdin in
let%lwt () = C.publish ~qos:C.Atleast_once ~topic:"topic-1" line client in
let%lwt () = Lwt_io.printl "Published." in
loop ()
in
loop ()
let () = Lwt_main.run (sub_example ())
```

0 comments on commit 59ca674

Please sign in to comment.