A lightweight MQTT broker in C for learning and experimentation.
This project implements a minimal MQTT v3.1.1 broker with multi-client handling via POSIX threads. It is intentionally simple and focused on core publish/subscribe behavior.
- MQTT v3.1.1 handshake (
CONNECT/CONNACK) - Multi-client support using one thread per client
SUBSCRIBEand topic-based message forwardingPUBLISHrouting to matching subscribers- Keep-alive via
PINGREQ/PINGRESP - Configurable broker limits in header files
- GCC (or compatible C compiler)
- POSIX-compatible system (Linux/Unix)
pthreadmake
Build:
makeRun:
./build/bin/mqtt_brokerThe broker listens on port 1883 by default.
If mosquitto_pub and mosquitto_sub are installed:
Terminal 1:
./build/bin/mqtt_brokerTerminal 2:
mosquitto_sub -h 127.0.0.1 -p 1883 -t demo/topicTerminal 3:
mosquitto_pub -h 127.0.0.1 -p 1883 -t demo/topic -m "hello broker"CONNECTCONNACKPUBLISHSUBSCRIBESUBACKPINGREQPINGRESPDISCONNECT
.
├── include/
│ ├── broker.h # Broker API and limits
│ └── mqtt.h # MQTT constants and packet handlers
├── src/
│ ├── main.c # TCP server and client threading
│ ├── broker.c # Client/subscription registry and routing
│ └── mqtt.c # MQTT packet parsing and handlers
├── build/ # Generated build output
├── Makefile # Build targets: all, run, clean
├── README.md
└── LICENSE
Defined in source headers:
MAX_CLIENTSininclude/broker.hMAX_SUBSCRIPTIONSininclude/broker.hMAX_TOPIC_LENininclude/broker.hMQTT_PORTininclude/mqtt.h
- No authentication/authorization
- No TLS support
- No retained messages
- No wildcard topic matching (
+,#) - Simplified QoS behavior intended for learning
Licensed under the MIT License. See LICENSE for details.