Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hyper-systems/ocaml-mqtt
MQTT client for OCaml
https://github.com/hyper-systems/ocaml-mqtt
iot mqtt ocaml protocol
Last synced: about 14 hours ago
JSON representation
MQTT client for OCaml
- Host: GitHub
- URL: https://github.com/hyper-systems/ocaml-mqtt
- Owner: hyper-systems
- Created: 2020-01-28T18:19:31.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-06T21:48:22.000Z (9 months ago)
- Last Synced: 2024-11-20T14:49:50.263Z (about 2 months ago)
- Topics: iot, mqtt, ocaml, protocol
- Language: OCaml
- Homepage: https://hyper-systems.github.io/ocaml-mqtt
- Size: 968 KB
- Stars: 18
- Watchers: 6
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# OCaml MQTT Client
[![OCaml-CI Build Status](https://img.shields.io/endpoint?url=https%3A%2F%2Fci.ocamllabs.io%2Fbadge%2Fhyper-systems%2Focaml-mqtt%2Fmaster&logo=ocaml)](https://ci.ocamllabs.io/github/hyper-systems/ocaml-mqtt)
This library implements the client MQTT v3 protocol.
* [API Documentation](https://hyper.systems/ocaml-mqtt/mqtt/index.html)
* [Issues](https://github.com/hyper-systems/ocaml-mqtt/issues)> Originally forked from https://github.com/j0sh/ocaml-mqtt.
## Quickstart
Install the packaage:
```
$ opam install mqtt
```In your dune project add the following dependencies to your dune file:
```lisp
(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:
```ocaml
module C = Mqtt_clientlet host = "127.0.0.1"
let port = 1883let 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) ] clientlet 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 ())
```