Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Application protocols: MQTT and CoAP

Study notes on the IoT application protocols MQTT and CoAP — how they work, key concepts, packet formats, examples, common exam mistakes and quick checks.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

IoT devices must exchange small messages with low power and sometimes poor networks. MQTT and CoAP are two lightweight application-layer protocols engineers use to connect sensors, actuators and cloud services. Knowing when to pick each, how their message flows provide reliability or low overhead, and how to secure them is essential when designing real IoT systems.

Key ideas

Definitions and exact statements:

  • Publish–subscribe model: publishers send messages to a topic (a named channel). Subscribers subscribe to topics and receive messages delivered by broker. Assumes: the broker stores subscription state for the duration of a session; persistence across reconnects depends on Clean Session (MQTT 3.1.1) or Clean Start plus Session Expiry (MQTT v5).
  • Topic filter: subscription pattern that can include wildcards: + (single-level) and # (multi-level). Clients receive messages where topic name matches the filter (exact matching rules defined in the MQTT specification). Wildcards are only allowed in subscriptions (topic filters), not in topic names used when publishing.
  • Quality of Service (QoS): three discrete levels defining delivery assurance between client and broker: QoS 0 (at most once — no acknowledgement), QoS 1 (at least once — requires PUBACK), QoS 2 (exactly once — four-step handshake PUBREC/PUBREL/PUBCOMP). Assumption: QoS only controls delivery between client and broker (not end-to-end if multiple brokers are chained). Effective delivery QoS to a subscriber is the minimum of the publisher's QoS and the maximum QoS granted in the subscriber's SUBSCRIBE.
  • Retained message: if a published message has the retained flag, broker stores it as the last known message for that topic and sends it immediately to future subscribers of that topic.
  • Last Will and Testament (LWT): a message stored by broker on CONNECT, published by the broker on behalf of a client if the broker detects an ungraceful disconnection. Also note: clients use Keep Alive with PINGREQ/PINGRESP to detect and close half-open TCP connections.

Draw to explain (what to draw): a two-column timeline with "Client" on left and "Broker" on right. For QoS 1 draw arrows: PUBLISH (packet id=10) -> PUBACK (packet id=10). Label fields: control packet type, remaining length, variable header (packet identifier), payload.

  • CoAP (Constrained Application Protocol): an HTTP-like RESTful application protocol designed for constrained nodes and networks, usually running over UDP (User Datagram Protocol). It maps to the REST model: resources identified by URIs, methods (GET, POST, PUT, DELETE), and response codes. CoAP assumes a message-oriented, possibly unreliable transport (UDP) and therefore uses its own lightweight confirmable messages and retransmission rules.

    Definitions and exact statements:

    • Confirmable (CON) message: CoAP message that requires an ACK from the recipient; retransmitted if no ACK arrives within a calculated timeout (exponential backoff). Assumption: retransmission stops after a limited number of attempts (default 4) to limit network load. Default parameters: ACK_TIMEOUT = 2 s, ACK_RANDOM_FACTOR = 1.5, MAX_RETRANSMIT = 4 (RFC 7252).
    • Non-confirmable (NON) message: sent without ACK; suitable for non-critical updates.
    • Message ID: 16-bit identifier used for duplicate detection and matching CON/ACK. Assumption: Message ID is chosen by the sender and wraps modulo 2^16.
    • Token: up to 8 bytes value placed in request and echoed in response to match responses to requests when multiple requests are outstanding.
    • Resource discovery: resources can be discovered via the /.well-known/core URI using CoRE Link Format (RFC 6690).
    • Observe option: allows server-to-client asynchronous updates by keeping an observation relationship; server pushes notifications when resource state changes.
    • Block-wise transfer: for large payloads CoAP splits messages into Block1 (requests) and Block2 (responses) blocks with an SZX field to indicate block size (0..6 => 2^(SZX+4) bytes).

    Draw to explain (what to draw): a three-column diagram with Client, Gateway/Proxy (optional), Server. Show CoAP CON GET (MsgID=100), Server ACK (MsgID=100) with payload, and a separate Observe notification flow with sequence numbers.

  • Security and proxies

    • DTLS (Datagram Transport Layer Security) is the usual security layer for CoAP to provide confidentiality and integrity over UDP. For MQTT, TLS over TCP is standard. Assumption: DTLS/TLS provide transport-level security but need certificate/key management or pre-shared keys for constrained devices. CoAP can also use OSCORE to provide end-to-end message protection through proxies.
    • HTTP–CoAP proxying: CoAP is designed to interwork with HTTP via proxies; mapping rules exist for methods and headers.
  • Design trade-offs (important for exams)

    • Use MQTT when you need a central broker, persistent sessions, ordered delivery and simple QoS semantics over reliable TCP (sensors that sleep and reconnect, mobile networks). Assumption: devices can support TCP.
    • Use CoAP when you need RESTful, lightweight UDP-based exchanges, multicast support, or direct client-server interactions with constrained memory/CPU.

Formulas

For this programming/protocol topic list the syntax and fixed-format fields to remember.

MQTT Fixed header byte: [ControlPacketType (4 bits) | Flags (4 bits)] — Control packet type identifies message (CONNECT=1, PUBLISH=3, PUBACK=4, PUBREC=5, PUBREL=6, PUBCOMP=7). Applies to MQTT v3.1.1 and v5 with nominal mapping.

MQTT Remaining Length: variable-byte integer (1-4 bytes) — encodes number of remaining bytes in packet; each byte uses 7 bits for data and MSB as continuation flag.

MQTT PUBLISH (variable header): [Topic Name (2-byte length + UTF-8 string)] [Packet Identifier (2 bytes) if QoS > 0] — Packet Identifier used for QoS 1 and 2 matching; unit: bytes. In MQTT v5, a Properties field follows (before the payload).

CoAP Header: 4 bytes = [Version (2 bits) | Type (2 bits) | Token Length (4 bits)] [Code (8 bits)] [Message ID (16 bits)] — Version normally 1; Type = {0: CON, 1: NON, 2: ACK, 3: RST}.

CoAP Token: 0-8 bytes — used to correlate responses to requests.

Default ports: MQTT cleartext TCP 1883, MQTT over TLS 8883, CoAP UDP 5683, CoAP over DTLS 5684 — standard default ports used unless changed.

Worked examples

Example 1 — MQTT QoS 1 publish (exam-style trace)

Problem: A sensor client publishes temperature "25.3" to topic sensors/room1/temp with QoS 1 and packet identifier 10. Show message exchange until broker confirms delivery to client.

Steps:

  1. Connection established earlier (TCP handshake + MQTT CONNECT/CONNACK). Assume connected.
  2. Client builds PUBLISH packet:
    • Fixed header: ControlPacketType=3 (PUBLISH), Flags: DUP=0, QoS=1, RETAIN=0.
    • Remaining length: computed for topic length + 2 bytes + packet id + payload (+ properties in MQTT v5, if present).
    • Variable header: Topic Name = length+UTF-8 (sensors/room1/temp), Packet Identifier = 10 (0x000A).
    • Payload: ASCII 25.3.
  3. Client sends PUBLISH(packet id=10).
  4. Broker receives and stores message (if necessary) and responds with PUBACK(packet id=10).
  5. Client receives PUBACK(10) and considers message delivered to broker. (Broker forwards to subscribers asynchronously.)

Final state: client has no retransmission pending for packet id 10. If PUBACK not received within client implementation timeout, client may retransmit PUBLISH with DUP flag set.

Example 2 — CoAP confirmable GET and ACK (exam-style trace)

Problem: A CoAP client issues a confirmable GET for resource /temp to server. Use Message ID 100 and Token 0x55.

Steps:

  1. Client sends CON GET message: Header(Version=1, Type=CON, TKL=1), Code=0.01 (GET), Message ID=100, Token=0x55, Uri-Path option temp.
  2. Server receives CON GET and must reply with either ACK (piggybacked) or ACK(empty) + separate NON/CON response. Here server replies immediately with piggybacked ACK containing payload 25.3:
    • ACK header: Type=ACK, Message ID=100, Token=0x55, Code=2.05 (Content), Payload 25.3.
  3. Client receives ACK with matching Message ID and Token and matches response to request.

If ACK does not arrive, client retransmits CON GET using the same Message ID until max retransmit limit.

Example 3 — Short model answer (theory) showing when to choose MQTT vs CoAP

Question: Compare MQTT and CoAP and state which you would use for a battery-powered sensor that must report occasional readings to a cloud service.

Model answer (points to include for full marks): define both protocols, state transport (MQTT over TCP, CoAP over UDP), QoS semantics (MQTT QoS 0/1/2; CoAP uses CON/NON and retransmission), overhead and session state (MQTT needs broker and can persist sessions; CoAP is stateless or uses observe for push), security options (TLS vs DTLS), multicast (CoAP supports IP multicast), and conclude: for reporting occasional readings to a cloud service over unreliable mobile network where reliable delivery and session persistence are needed, prefer MQTT with QoS 1 and TLS; if multicast or extreme resource constraints exist and REST mapping is required, use CoAP.

Common mistakes

  • Thinking MQTT QoS 2 guarantees application-level end-to-end deduplication across multiple brokers — QoS applies only between client and one broker.
  • Assuming CoAP is secure by default — CoAP is plain UDP unless DTLS (or OSCORE) is used.
  • Forgetting Message ID vs Token in CoAP: Message ID is for message-level ACKs/duplicate detection; Token correlates request->response pairs at the application layer.
  • Not including packet identifiers in MQTT PUBLISH variable header when QoS > 0.

For your exam

Kinds of questions you will see

  • Definitions and short notes (define MQTT, CoAP, QoS, CON/NON, Token, Message ID).
  • Diagrams/traces (draw packet exchanges for MQTT QoS 1 or CoAP CON/ACK). Include labels for fields (packet id, topic, message id, token).
  • Comparison (choose protocol for a use case and justify choice with transport, overhead, QoS and security).
  • Derivations/counts (calculate number of packets exchanged for MQTT QoS 2 publish between client and broker: PUBLISH->PUBREC->PUBREL->PUBCOMP = 4 application-layer packets, plus TCP segments and ACKs at transport layer if TCP used).
  • Short code-like syntax (write a simple MQTT connect sequence or a CoAP GET). Note: exams may ask for option-value mapping and default ports.

What a full-marks answer includes

  • Clear definitions of terms, correct protocol names and transport. If asked for diagrams, draw timelines with actors and label each arrow with packet type and key identifiers. State assumptions (e.g., client has open TCP connection). For comparisons, list at least three relevant criteria (reliability, statefulness, security, multicast, overhead) and draw a concise conclusion.

Quick check

  1. What MQTT QoS level causes a PUBREC/PUBREL/PUBCOMP exchange?
  2. What CoAP message type requires an ACK?
  3. Default UDP port for CoAP and default TCP port for MQTT (cleartext)?

Answers: 1. QoS 2. 2. Confirmable (CON). 3. CoAP 5683 (UDP), MQTT 1883 (TCP).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?