IoT protocols and messaging
How MQTT, CoAP, LwM2M, HTTP and WebSockets move data between devices and back ends, which payload format to pick, and why a device needs the right time before it can trust a server.
Overview
Once a device has a network connection, it still needs an agreed way to send readings, receive commands and be managed. That is the job of the messaging layer. This hub is for makers deciding how a sensor should talk to a broker or a dashboard, and for product teams choosing a protocol they will have to support across a fleet for years. It covers MQTT (a publish/subscribe protocol), CoAP (the Constrained Application Protocol), LwM2M (Lightweight Machine to Machine, a device-management layer), HTTP (Hypertext Transfer Protocol) and WebSockets, the payload formats that ride on them, and the clock every secure connection depends on.
The choice follows from the link (Wi-Fi, Thread, NB-IoT), whether the device sleeps, who else must read the data, and whether you need standard remote management.
Key ideas
MQTT puts a broker in the middle
Devices publish messages to topics on a broker, and anything subscribed to a matching topic filter receives them. Filters use + for one topic level and # for any number of trailing levels. MQTT defines three quality of service (QoS) levels: 0 at most once, 1 at least once, 2 exactly once. QoS applies to each sender-to-receiver hop, so the broker may deliver to a subscriber at a different QoS from the one the publisher used. A retained message replaces the previous one for that topic and is sent to new subscribers (MQTT 5.0 lets a subscriber opt out), and a retained message with an empty payload deletes it. A Will message is published by the broker when a client’s connection closes without a clean disconnect, which makes it a simple way to mark a device offline. If the broker hears nothing for one and a half times the keep-alive interval, it must drop the connection.
MQTT needs an ordered, lossless transport. The specification names TCP (Transmission Control Protocol), TLS (Transport Layer Security) and WebSocket, says UDP (User Datagram Protocol) is not suitable on its own, and notes the ports registered with IANA (the Internet Assigned Numbers Authority): 1883 (plain) and 8883 (TLS). Two versions are in use. MQTT 3.1.1 became an OASIS Standard on 29 October 2014, with errata approved on 10 December 2015. MQTT 5.0 followed on 7 March 2019 and adds reason codes, session expiry, message expiry, topic aliases, shared subscriptions and a request/response pattern with response topics and correlation data.
CoAP brings web-style requests to constrained networks
CoAP (RFC 7252) was designed for constrained nodes and lossy networks. It uses a 4-byte fixed header and the methods GET, POST, PUT and DELETE. Each message is either confirmable, retransmitted with exponential back-off until acknowledged or a retry limit is reached, or non-confirmable. The default port is UDP 5683, and coaps on port 5684 runs over DTLS (Datagram TLS). Extensions fill the gaps: Observe (RFC 7641) lets a client subscribe to a resource, block-wise transfer (RFC 7959) carries payloads such as firmware images without IP fragmentation, RFC 8323 runs CoAP over TCP, TLS and WebSockets, and OSCORE (Object Security for Constrained RESTful Environments, RFC 8613) protects messages end to end through proxies.
LwM2M adds device management on top
LwM2M, from OMA SpecWorks, defines four interfaces (bootstrap, client registration, device management and service enablement, information reporting) and a library of numbered objects, such as Firmware Update (object 5). Version 1.0 ran over CoAP on UDP or SMS text messages. Version 1.1 added CoAP over TCP, non-IP transport for cellular IoT and LoRaWAN, and OSCORE. Version 1.2 added HTTP and MQTT bindings. The current release is 1.2.2, approved on 13 June 2024. Open-source stacks lag the specification: Zephyr’s client implements 1.0.2 by default and 1.1.1 as an option, and Eclipse Leshan’s stable 1.x line targets 1.0.x. Check which version both ends speak before you depend on a feature.
HTTP and WebSockets
HTTP (RFC 9110) is a stateless request/response protocol on TCP port 80, or 443 with TLS. It suits a device that wakes, posts a reading and sleeps. The server only answers requests, so it cannot push to a sleeping device. WebSocket (RFC 6455) upgrades an HTTP connection into a two-way channel over TCP. MQTT can run inside it, using binary frames and the subprotocol name mqtt, which is how browser dashboards talk to a broker.
Payload formats: JSON, CBOR, SenML
JSON (JavaScript Object Notation, RFC 8259) is readable and supported everywhere, and JSON exchanged between systems must be UTF-8. CBOR (Concise Binary Object Representation, RFC 8949, Internet Standard 94) is a binary format designed for small code size and fairly small messages, which matters on narrowband links. SenML (Sensor Measurement Lists, RFC 8428) defines standard field names for sensor readings in both JSON and CBOR, so a back end can parse readings from devices it did not design.
Get the time before you trust a server
Every X.509 certificate carries a validity period, notBefore to notAfter. A device that boots with its clock at zero cannot check those dates, so TLS validation fails or, worse, gets switched off. The Network Time Protocol (NTP) version 4, RFC 5905, runs on UDP port 123, and Simple NTP (SNTP), a subset meant for clients with a single upstream server, is enough for most devices. Sync the clock before the first TLS handshake, and decide what the device does when no time server answers.
Where to start
Join a network first with /learn/beginner/06-wifi-and-your-first-request/, which makes an HTTP request, then work through /learn/beginner/07-mqtt-basics/ with a local Mosquitto broker and /learn/beginner/09-a-local-dashboard/. See also the MQTT cheat sheet and protocol matrix. For narrowband links see /topics/cellular-iot/. Certificates and TLS are covered on /topics/iot-security/, firmware delivery on /topics/ota-and-fleet-management/, and MQTT Sparkplug on /topics/industrial-iot/.
Tools and platforms
| Job | Option | What the source says | Source |
|---|---|---|---|
| Local MQTT broker | Eclipse Mosquitto | Implements MQTT 5.0, 3.1.1 and 3.1, EPL/EDL licensed | mosquitto.org |
| MQTT client in Python on a host | paho-mqtt 2.2.0 (4 November 2026) | MQTT 5.0, 3.1.1 and 3.1, Python 3.9 or later | PyPI |
| MQTT on an ESP32 with ESP-IDF v6 | ESP-MQTT, managed component espressif/mqtt |
Moved out of ESP-IDF in v6.0. MQTT 3.1.1 and 5.0 over TCP, TLS, WebSocket and WebSocket Secure | ESP-IDF v6.1.1 docs, esp-mqtt |
| CoAP in C | libcoap 4.3.5 | RFC 7252, 7641, 7959, 8323, 8613 and more | libcoap.net |
| CoAP in Python | aiocoap | asyncio-based, Python 3.11 or later, full OSCORE client support | aiocoap docs |
| LwM2M server for testing | Eclipse Leshan | Java server and client. The stable 1.x line targets LwM2M 1.0.x, 2.x targets 1.1.x and is in development | Leshan |
| LwM2M client on Zephyr | Zephyr LwM2M library | LwM2M 1.0.2 default, 1.1.1 optional, DTLS, queue mode, Firmware Update object | Zephyr docs |
| Readings a third party can parse | SenML in JSON or CBOR | Standard names for measurements | RFC 8428 |
For makers
Run Mosquitto on a laptop or a Raspberry Pi, publish JSON at QoS 1, keep a retained status topic per device, and set a Will message that writes offline to it. No cloud account is needed.
For product teams
Choose by link and by management needs. On NB-IoT or Thread, CoAP over UDP with DTLS or OSCORE avoids TCP. If you need standard remote management and firmware objects, LwM2M gives you a data model, but confirm the version both ends implement. On MQTT, choose 5.0 for reason codes and session expiry, and give every device its own credentials. Use CBOR or SenML when bytes cost money.
Common mistakes
- Using QoS 2 everywhere. The specification warns of its extra overhead, and QoS is per hop, not end to end.
- Leaving a stale retained message on a topic. Publish an empty retained payload to clear it.
- Running MQTT over raw UDP. The specification says UDP is not suitable on its own.
- Starting TLS before the clock is set, then disabling certificate checks to make it work.
- Assuming a library “supports LwM2M” means version 1.2. Zephyr and Leshan stable target 1.0.x and 1.1.x.
- Upgrading an ESP-IDF project to v6 without adding the
espressif/mqttdependency. The component is no longer bundled. - Sending JSON with non-UTF-8 strings to a back end that follows RFC 8259.
Going deeper
- MQTT Version 5.0, OASIS Standard, 7 March 2019, clauses 3.3.1.3 (retain), 4.3 (QoS) and 4.7 (topics).
- RFC 7252, CoAP, with RFC 7641, RFC 7959 and RFC 8613.
- LwM2M 1.2.2 Core and Transport technical specifications.
- RFC 8949, CBOR and RFC 8428, SenML.
- RFC 5905, NTPv4, section 14 for SNTP.