MQTT 3.1 and 5 in OCaml using Eio
HTML 95.7%
OCaml 4.2%
Dune 0.1%
Other 0.1%
12 1 0

Clone this repository

https://tangled.org/anil.recoil.org/ocaml-mqtte
git@git.recoil.org:anil.recoil.org/ocaml-mqtte

For self-hosted knots, clone URLs may differ based on your setup.

README.md

mqtte#

An MQTT protocol library for OCaml with an Eio-based client.

Features#

  • MQTT v3.1.1 and v5.0 support - Full protocol implementations for both versions
  • Effects-based I/O - Built on Eio for modern OCaml 5+ concurrency
  • QoS levels - Support for all three quality of service levels (0, 1, 2)
  • Topic wildcards - Full support for single-level (+) and multi-level (#) wildcards
  • Connection pooling - Integration with conpool for efficient connection management
  • TLS support - Secure connections via tls-eio
  • Command-line tools - Cmdliner integration for easy CLI application building

Installation#

opam install mqtte

Usage#

Publisher Example#

open Mqtte_eio.Client

let () =
  Eio_main.run @@ fun env ->
  Eio.Switch.run @@ fun sw ->

  let config = default_config ~client_id:"my-publisher" () in

  let client = connect
    ~sw
    ~net:(Eio.Stdenv.net env)
    ~clock:(Eio.Stdenv.clock env)
    ~config
    ~host:"127.0.0.1"
    ~port:1883
    ()
  in

  publish ~qos:`At_least_once ~topic:"sensors/temperature" "22.5" client;
  disconnect client

Subscriber Example#

open Mqtte_eio.Client

let () =
  Eio_main.run @@ fun env ->
  Eio.Switch.run @@ fun sw ->

  let config = default_config ~client_id:"my-subscriber" () in

  let on_message msg =
    Printf.printf "Received [%s]: %s\n" msg.topic msg.payload
  in

  let client = connect
    ~sw
    ~net:(Eio.Stdenv.net env)
    ~clock:(Eio.Stdenv.clock env)
    ~on_message
    ~config
    ~host:"127.0.0.1"
    ~port:1883
    ()
  in

  subscribe ~qos:`At_least_once ["sensors/#"] client;
  Eio.Time.sleep (Eio.Stdenv.clock env) 60.0;
  disconnect client

Library Structure#

  • mqtte - Core protocol types and codecs (no I/O dependencies)
  • mqtte.eio - Full client library with Eio-based transport

Documentation#

API documentation is available at: https://tangled.org/@anil.recoil.org/ocaml-mqtte

License#

ISC License. See LICENSE.md for details.