Skip to content
IoT Crest

MQTT vs CoAP: which IoT messaging protocol to use

Published , 6 min read

Short answer: Use MQTT when many devices report to many consumers through a broker over a reliable link; use CoAP when a small or sleepy device needs request-response or device-to-device messaging over UDP with no broker. Most fleets end up with MQTT at the center and CoAP, if anywhere, at the constrained edge.

MQTT vs CoAP compared for IoT devices: publish-subscribe over TCP against a REST-like protocol over UDP, delivery guarantees, power and when each one fits.

This article covers what each protocol does, the differences that decide between them, and where each one fits in a real system. It does not cover MQTT-SN, broker configuration, or CoAP's more specialized extensions beyond observe and block-wise transfer.

MQTT and CoAP are the two messaging protocols built for small devices, and they solve the same problem from opposite directions. One is a publish-subscribe protocol that needs a broker and a reliable connection. The other is a request-response protocol modeled on the web that runs over UDP and needs nothing in the middle. The choice follows from the shape of the communication, and this article sets out that shape for each.

How MQTT works

MQTT is a publish-subscribe protocol standardized by OASIS (MQTT 5.0; version 3.1.1 is still widely deployed). Every client connects to a broker over a long-lived TCP connection. A publisher sends a message to a named topic; the broker delivers it to every client subscribed to that topic. Publishers and subscribers never know about each other.

The features that matter for small devices are its three quality-of-service levels (fire and forget, at least once, exactly once), retained messages that give a new subscriber the last known value, a last-will message the broker publishes if a client vanishes, and persistent sessions that queue messages for a client that is away. The MQTT explainer goes through each.

MQTT’s cost is the connection. TCP needs a handshake and state on both ends, TLS adds another handshake, and keeping the connection alive needs periodic traffic. That is fine for a device that is always on and cheap for a device that connects, sends and disconnects, but it is real work for a device that wakes for a second every ten minutes.

How CoAP works

CoAP, the Constrained Application Protocol, is defined by the IETF in RFC 7252. It is a request-response protocol with the same shape as HTTP: a client sends GET, PUT, POST or DELETE to a resource identified by a URI on a server, and the server replies with a response code and a payload. The difference is that it runs over UDP with a compact binary header, so a request fits in a single small datagram and a device with a few kilobytes of memory can implement it.

Because UDP does not guarantee delivery, CoAP has its own reliability: a request can be marked confirmable, in which case the sender retransmits until it gets an acknowledgment, or non-confirmable, in which case it is sent once. The observe extension turns request-response into notification: a client registers interest in a resource and the server sends a message whenever it changes, which gives CoAP a lightweight version of what MQTT does with subscriptions. Block-wise transfer carries payloads larger than one datagram, such as a firmware image, in numbered pieces.

Security is DTLS, the datagram version of TLS. CoAP also has a mapping to TCP and WebSockets (RFC 8323) for networks that block UDP, though that gives up some of the reasons for choosing it.

CoAP’s cost is the absence of a middle. Every client must be able to reach every server it talks to, which is simple on a local network and complicated across the internet, where devices sit behind address translation and firewalls. A proxy or a gateway usually fills that gap, and at that point the design starts to look like it has a broker after all.

MQTT: broker in the middle, TCP broker publish to a topic; broker fans out long-lived connections, QoS, retain, will CoAP: direct, UDP client server GET /temp 2.05 content observer sensor observe: notified on change one datagram per message, retries per request
Diagram: MQTT clients publish to a broker that fans messages out to subscribers over long-lived TCP connections; CoAP clients send single-datagram requests straight to a server over UDP, with the observe extension providing notifications.

MQTT vs CoAP: the differences that matter

MQTTCoAP
ModelPublish-subscribe through a brokerRequest-response, client to server, plus observe
TransportTCP (TLS for security)UDP (DTLS for security); TCP mapping exists
Middle boxBroker requiredNone required; proxy optional
Delivery guaranteesQoS 0, 1, 2 per messageConfirmable or non-confirmable per message
State per deviceA connection held open, or reconnected each timeNone between requests
Message sizeSmall header; payloads up to large limitsOne datagram by default; block-wise for larger
DiscoveryNone built in; topics are conventionResource discovery built in
Fan-out to many consumersNativeNot native; needs a proxy or the application
Works through NAT and firewallsYes, device connects outwardAwkward without a proxy
SuitsFleets reporting to back ends, dashboards, alertsConstrained devices, local networks, device-to-device

The decisive rows are the model, the transport and the middle box. MQTT assumes many producers and many consumers with a broker between them, over links that can hold a connection. CoAP assumes a client that knows which server it wants, over links where a single datagram is all it can afford. Everything else in the table follows from those three.

Power is a consequence rather than a row of its own. A CoAP device can send one datagram and sleep, with no connection to set up or tear down. An MQTT device pays for a TCP and TLS handshake on each connection, which for a sleepy device is most of its awake time; MQTT with a persistent session and a long keep-alive reduces that cost if the device can afford to stay connected. On a mains-powered device the difference is irrelevant.

When to use MQTT

Use MQTT when the system is a fleet reporting to a back end: sensors and gateways publishing readings, and a historian, a dashboard, an alert engine and an analytics service all subscribing. The broker is what makes adding a consumer free, and that property is worth the connection cost. It is the right choice for gateways, for mains-powered devices, for cellular devices that can hold a connection, and for anything where the consumers are not known in advance.

It is also the protocol the rest of the world expects. Cloud IoT services, industrial gateways and home controllers all speak it, and the industrial pattern of OPC UA at the machine and MQTT above it, described in the OPC UA and MQTT article, is built on it. A device that speaks MQTT plugs into that world with no translation.

When to use CoAP

Use CoAP when the device is genuinely constrained (a few kilobytes of memory, a battery, a radio that carries one small datagram at a time) and the communication is request-response or device-to-device on a local network. A sensor that a gateway polls, an actuator that a controller commands, a group of devices on a Thread network talking to each other: all fit CoAP’s shape. Thread and 6LoWPAN networks in particular were designed with CoAP in mind, and several device-management standards for cellular IoT devices run over it, which is why NB-IoT modules often speak CoAP before they speak MQTT.

CoAP also fits where a broker would be a single point of failure the site cannot afford, or where the only network is the local one and there is nothing to fan out to.

Using MQTT and CoAP together

The two are not exclusive, and the usual arrangement is layered. Constrained devices speak CoAP to a gateway on the local network; the gateway speaks MQTT to the broker on behalf of all of them. The devices get the protocol built for their limits, the back end gets the fan-out and the ecosystem, and the gateway does the translation once. That is the same pattern the edge computing article describes for processing: do the constrained work close to the device and hand a cleaner stream upward.

local network constrained devices gateway translates once CoAP in, MQTT out CoAP over UDP: one datagram, then sleep MQTT over TCP broker fan-out historian dashboard alert engine CoAP at the constrained edge; the back end gets the fan-out and the ecosystem
Diagram: the layered arrangement that uses both protocols. Constrained devices on the local network speak CoAP over UDP to a gateway; the gateway speaks MQTT over TCP to the broker on their behalf, and the broker fans the readings out to the historian, dashboard and alert engine.

Whichever you choose, the protocol is the smaller decision. Payload format, topic or resource naming, security credentials and firmware update paths are what a fleet lives with for years, and both protocols leave all of those to you. Pick the protocol from the shape of the communication, then spend the effort where it lasts.

Written by the IoT Crest editorial team. Facts in this article follow the editorial policy: only well-established behavior is stated and no figures are invented.

More from Connectivity

  1. What is LPWAN? Low-power wide-area networks

    What LPWAN is, what it gives up for range and battery life, how NB-IoT, LTE-M, LoRaWAN and Sigfox differ, who runs each network and how to choose.

  2. 5G RedCap: what it is and where it fits

    5G RedCap explained: what Reduced Capability 5G cuts from full 5G, how it compares with LTE-M, NB-IoT and Cat 1 bis, eRedCap, and which devices should use it.

  3. Wi-Fi HaLow (802.11ah): range, speed and uses

    Wi-Fi HaLow explained: how 802.11ah uses sub-1 GHz bands for long range, what speed and battery life to expect, how it compares with LoRaWAN, and where it fits.

  4. What is an IMEI number? Digits, checks and uses

    What is IMEI: the 15-digit ID of a phone or cellular module, how its TAC, serial and check digit work, how it differs from IMSI and ICCID, and where to find it.

  5. What is IMSI? The international mobile subscriber identity

    What is IMSI: the subscriber identity on every SIM, its MCC, MNC and MSIN digits, how networks hide it with temporary IDs and SUCI, and IMSI vs IMEI vs ICCID.

  6. What is an MNO? Mobile network operators vs MVNOs

    MNO meaning in telecoms: what a mobile network operator owns, MVNOs, MVNEs and MVNAs, what a PLMN is, and how to compare an MNO and an MVNO for IoT SIMs.

  7. 2.4 GHz vs 5 GHz: which Wi-Fi band IoT devices need

    2.4 GHz vs 5 GHz Wi-Fi for IoT: range, speed, channels 1, 6 and 11, why smart devices need 2.4 GHz, and how to fix pairing on a dual-band router.

  8. ICCID number explained: the SIM serial, digit by digit

    The ICCID number explained: what the SIM serial is, how its digits break down, how it differs from IMSI and IMEI, and where to find it on phones and modules.

  9. What is an M2M SIM card? How M2M and IoT SIMs differ from phone SIMs

    What an M2M or IoT SIM card is, how it differs from a regular phone SIM, where it is used, activation, the 2FF to MFF2 form factors, M2M eSIM and iSIM.

  10. IoT protocols explained: the radio, network and messaging layers

    IoT protocols explained layer by layer: radio and link protocols, the network and transport layers above them, messaging on top, and how to combine them.

  11. MQTT explained: topics, QoS levels and retained messages

    MQTT explained: how a broker, publishers and subscribers fit together, how to name topics and use wildcards, what QoS levels promise and retained messages.

  12. LoRaWAN explained: range, data rates and when to use it

    LoRaWAN explained: how LoRa radio and the LoRaWAN network layer differ, what range to expect, how spreading factors work, and when to pick it.

  13. NB-IoT vs LTE-M for cellular IoT devices

    NB-IoT vs LTE-M for cellular IoT devices: bandwidth and latency, mobility, power saving modes, coverage inside buildings, and which one to pick.

All connectivity articles

Sponsor an article in this topic

Sponsored articles and in-article link placements for IoT vendors, agencies and SEO buyers. Every sponsored piece is labeled, edited to the same standard as the rest of the site, and placed in the hub its readers already visit. Rates on request.

Ask a question

Ask a question

Spotted an error, or want a topic covered? It goes straight to the editors, who reply by email, WhatsApp or SMS.