notesonly.in

One notebook for every subject — open it anywhere.

Log in

MQTT and CoAP Protocols: Complete Beginner's Guide

MQTT and CoAP are the two most popular messaging protocols built for the Internet of Things. MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe protocol: devices publish messages to named topics on a central broker, which delivers them to subscribers. CoAP (Constrained Application Protocol) is a lightweight request/response protocol — like a slimmed-down HTTP for tiny devices, usually over UDP.

It matters because ordinary web protocols are too heavy for battery-powered sensors.

Key Concepts

  • Broker (MQTT) — the central server receiving published messages and forwarding them to subscribers. Devices never talk to each other directly.
  • Topic (MQTT) — a named channel like "home/kitchen/temp". Wildcards like "home/+/temp" let one subscription catch many topics.
  • QoS levels (MQTT) — 0 = fire and forget (at most once), 1 = acknowledged (at least once, may duplicate), 2 = exactly once (safest, most overhead).
  • Retain and Last Will (MQTT) — a retained message is stored for new subscribers; a last-will message fires automatically if a device disconnects unexpectedly.
  • CoAP methods — GET, POST, PUT, DELETE mirroring HTTP, plus observe, which subscribes a client to updates on a resource.
  • UDP vs TCP — CoAP usually runs over UDP (no connection setup, tiny overhead); MQTT runs over TCP (reliable, ordered). UDP suits loss-tolerant readings; TCP suits commands that must arrive.

Formulas

  • MQTT message size (QoS 1): ≈ 2 (fixed header) + 2 (topic length) + topic length + 2 (packet ID) + payload. A 24-byte payload on topic "home/temp" (9 chars) = 2 + 2 + 9 + 2 + 24 = 39 bytes on the wire.
  • CoAP message size: ≈ 4 (header) + token (0–8 bytes) + options + 1 (payload marker) + payload — typically about 35 bytes for the same 24-byte reading.
  • Daily traffic: bytes per day = message size × messages per day. The 39-byte MQTT message sent every 10 seconds (8,640/day) uses 39 × 8,640 ≈ 329 KB per day.

Worked Example

A soil-moisture sensor publishes a 24-byte reading every 10 seconds over MQTT QoS 1 to topic "farm/soil" (9 characters). How many bytes per message, and per day?

  1. Per message: 2 (header) + 2 (topic length) + 9 (topic) + 2 (packet ID) + 24 (payload) = 39 bytes.
  2. Messages per day = 86,400 / 10 = 8,640.
  3. Daily data = 8,640 × 39 ≈ 329 KB per day — small enough for even a 2G link, which is why MQTT is the default for such sensors.

Common Misconceptions

  • "MQTT is always better than CoAP." MQTT wins for one-to-many updates through a broker; CoAP wins for simple device-to-device requests on extremely constrained hardware with no always-on broker.
  • "These protocols are encrypted by default." Neither is. Security comes from MQTT over TLS and CoAP over DTLS — without that, anyone on the network can read or spoof messages.

Real-World Applications

  • Smart homes: MQTT brokers connect lights, thermostats and sensors to apps like Home Assistant.
  • Smart meters: electricity and water meters use CoAP over low-power networks to report usage with minimal energy.
  • Connected vehicles: fleets publish GPS and engine data over MQTT so dispatchers track every truck in real time.

MQTT gives you reliable publish/subscribe messaging through a broker; CoAP gives you featherweight HTTP-style requests for the tiniest devices. Pick MQTT when many subscribers need the same updates, CoAP when a device just asks and answers — and always add TLS or DTLS before real data flows.