The idea: HTTP-style requests that fit in one small datagram
A temperature sensor runs on a battery and a tiny chip. It cannot afford a TCP connection and HTTP text headers for every reading. CoAP (Constrained Application Protocol, RFC 7252) keeps the HTTP idea, a client asks GET /temperature and the server answers 2.05 Content, but packs each message into one UDP datagram with a 4-byte header. The default port is UDP 5683. Since UDP does not resend anything, CoAP does its own acknowledgements and retransmissions, but only for messages that ask for them.
The 4-byte header
Every CoAP message starts with the same 32 bits. After them come the token, the options and, after a byte FF, the payload. The canvas shows each field of the last message and its bytes.
| Field | Bits | Values on this page |
|---|---|---|
| Ver | 2 | always 1 |
| T (type) | 2 | 0 CON, 1 NON, 2 ACK, 3 RST |
| TKL (token length) | 4 | 4 bytes, or 0 for an empty message |
| Code | 8 | class.detail: 0.01 GET, 2.05 Content, 0.00 Empty |
| Message ID | 16 | 0x7D34, 0x7D35 … from the client; 0x2C10 … from the sensor |
The code is written c.dd: 3 bits of class and 5 of detail. Class 0 is a request (0.01 GET, 0.02 POST, 0.03 PUT, 0.04 DELETE), class 2 a success, 4 a client error (4.04 Not Found), 5 a server error. So 2.05 is the byte 010 00101 = 0x45. Options are numbered: Uri-Path is 11, Content-Format 12, Observe 6. Each one is sent as a small delta from the previous option's number plus a length, which is why /temperature costs only one byte more than its letters.
Message ID and token: two IDs, two jobs
- The Message ID works at the message layer. An ACK or RST carries the Message ID of the message it answers. A resent message keeps its Message ID, so a receiver that sees the same ID twice knows it is a duplicate and sends the same answer again instead of doing the work twice.
- The token works at the request layer. A response carries the token of its request. That still works when the response comes in a different message with a different Message ID (Demo: separate response) or many times over (Demo: Observe).
CON, ACK and retransmission
A Confirmable (CON) message must be answered with an ACK carrying the same Message ID. The sender starts a timer of ACK_TIMEOUT = 2 s. If no ACK comes in time, it sends the same message again and doubles the timeout, at most MAX_RETRANSMIT = 4 times (Demo: a lost CON is resent). With a 2 s start, the tries go out at t = 0, 2, 6, 14 and 30 s, and the sender gives up at 62 s (Demo: every try lost).
Real clients pick the first timeout at random between 2 s and 3 s (ACK_RANDOM_FACTOR = 1.5), so that many devices that lost the same packet do not all resend at once. The page always uses 2 s, to keep the numbers round. The worst case with 3 s is MAX_TRANSMIT_WAIT = 93 s.
Piggybacked or separate response
When the server can answer at once, it puts the response in the ACK: type ACK, code 2.05, the token, the payload. One round trip, two datagrams (Demo: CON GET, answer piggybacked in the ACK). When the answer takes longer, holding the ACK back would make the client resend. So the server sends an empty ACK at once (code 0.00, no token, 4 bytes) and later a separate response in its own CON, with a new Message ID and the same token. The client ACKs that one.
Non-confirmable messages
A NON message is never acknowledged and never resent (Demo: NON request and response, a lost NON). It still has a Message ID, so duplicates can be spotted. NON suits a reading that is sent again soon anyway: losing one is cheaper than waiting for ACKs. A response to a NON request is usually a NON too.
An RST (Reset) is the "I cannot use this" answer: an empty message with the Message ID of a CON or NON the receiver does not want or cannot process.
Observe: the sensor pushes readings
Asking every few seconds wastes energy. With Observe (RFC 7641), the client sends one GET with the option Observe: 0 ("register"). The sensor answers as usual and adds the client to the resource's list of observers. For each new reading it sends a notification: a 2.05 response with the same token, a new Message ID and an Observe option holding a sequence number that goes up with each notification. The client drops a notification whose number is older than the last one it has, since UDP can reorder datagrams.
Notifications can be NON or CON. The sensor sends a CON now and then: an ACK tells it the client is still there. To stop, the client can simply forget the token and answer the next notification with RST; the sensor then removes it (Demo: Observe, notifications until RST). It can also send a GET with Observe: 1.
CoAP, HTTP and MQTT
| CoAP | HTTP/1.1 | MQTT | |
|---|---|---|---|
| Transport | UDP 5683 (DTLS 5684) | TCP 80 (TLS 443) | TCP 1883 (TLS 8883) |
| Style | request / response, REST | request / response, REST | publish / subscribe through a broker |
| Before the first request | nothing | TCP handshake | TCP handshake, CONNECT / CONNACK |
| GET /temperature | 20 bytes | about 50 bytes of text, plus TCP | no GET: SUBSCRIBE to a topic |
| Reliability | CON + ACK, resend with backoff | from TCP | from TCP, plus QoS 0 / 1 / 2 |
| Push | Observe | none (polling, SSE, WebSocket) | every message is a push |
CoAP talks to the device itself, and it maps to HTTP well enough that a proxy can translate between the two. MQTT puts a broker in the middle, which suits many devices sending to many readers. See also MQTT QoS for the broker version of "deliver at least once".
See also an HTTP connection from TCP handshake to close for what CoAP saves, TCP vs UDP for what UDP leaves to the application, and retry backoff with jitter for the same doubling idea in other systems.
What the page leaves out
DTLS security (coaps on port 5684) and OSCORE, the random first timeout between 2 and 3 s, duplicate detection when only the ACK is lost (the server resends its stored answer), NSTART and congestion control, blockwise transfer for large bodies (RFC 7959), multicast discovery and /.well-known/core, CoAP over TCP and WebSockets (RFC 8323), POST, PUT and DELETE, option values longer than 12 bytes, Max-Age and ETag, and the Observe rules for reordered notifications in detail.