A small, portable MQTT 3.1.1 client library implemented in strict C23, with examples and tests.
Highlights
- Strict C23 codebase (
nullptr,auto,constexpr) with-Wall -Wextra -Wpedantic -Werror. - Single public header:
include/mqtt/mqtt.h. - Platform abstraction in
include/mqtt/mqtt_pal.h, with an override hook. - Examples for TCP, reconnects, and TLS (OpenSSL).
- Thread-safe API suitable for single-threaded and multi-threaded clients.
Requirements
- A C23-capable compiler (
-std=c23or-std=c2x). - POSIX sockets and pthreads for the default PAL and examples.
- OpenSSL and
pkg-configfor the TLS example. - Optional:
justor Zig for build convenience.
Build With just
just
just test
just build-examplesDebug with sanitizers:
DEBUG=1 just testOutputs land in bin/.
Build With Zig
zig build
zig build test
zig build examplesEnable sanitizers in Debug builds:
zig build -Doptimize=Debug -Dsanitize=trueOutputs land in zig-out/bin/.
Quick Usage Sketch
#include "mqtt/mqtt.h"
int main() {
struct mqtt_client client;
uint8_t sendbuf[2'048];
uint8_t recvbuf[1'024];
mqtt_pal_socket_handle sockfd = /* open socket */;
mqtt_init(&client, sockfd, sendbuf, sizeof(sendbuf), recvbuf, sizeof(recvbuf),
nullptr);
mqtt_connect(&client, nullptr, nullptr, nullptr, 0, nullptr, nullptr,
MQTT_CONNECT_CLEAN_SESSION, 400);
while (true) {
mqtt_sync(&client);
}
}See examples/ for complete programs using POSIX or OpenSSL sockets.
Tests
just testorzig build testrunstesting/tests.c.- Tests and examples default to
test.mosquitto.organd require network access.
Platform Abstraction
- The default PAL lives in
include/mqtt/mqtt_pal.handsrc/mqtt_pal.c. - To supply your own PAL, define
MQTTC_PAL_FILEto a header that provides the required types, macros, and function declarations:
cc -DMQTTC_PAL_FILE=my_mqtt_pal.h ...Directory Layout
include/mqtt/mqtt.hpublic API.src/mqtt.ccore client.src/mqtt_pal.cplatform helpers.examples/example clients.testing/tests.cunit and protocol tests.
License
MIT. See LICENSE.