Ir para o conteúdo principal
Ir para o conteúdo principal

Connectivity

CoAP

CoAP (Constrained Application Protocol) is a REST-like web protocol over UDP for highly constrained devices, defined in IETF RFC 7252.

CoAP (Constrained Application Protocol) is an IETF web protocol for devices with little memory and power that work on lossy networks. Defined in RFC 7252, it follows the REST model of HTTP (resources, URIs and the methods GET, POST, PUT and DELETE), but it runs over UDP and keeps messages small with a 4-byte fixed header.

How CoAP works

A CoAP client sends a request to a resource on a server, identified by a URI such as coap://sensor.local/temperature, and gets back a response code such as 2.05 (Content) or 4.04 (Not Found). The default port is UDP 5683; the secure variant, coaps, uses DTLS on port 5684. Other concepts worth knowing:

  • Message types: a Confirmable (CON) message is retransmitted with exponential back-off until the receiver returns an Acknowledgement (ACK); a Non-confirmable (NON) message is sent once.
  • Observe (RFC 7641): the client registers with a resource and the server sends a notification each time it changes, so the client doesn't have to poll.
  • Block-wise transfer (RFC 7959): large payloads travel in blocks across several request/response pairs.
  • Discovery: a GET to /.well-known/core returns the list of resources a device offers.
  • Proxies can translate between CoAP and HTTP, and CoAP supports multicast requests.

RFC 8323 (2018) defines CoAP over TCP, TLS and WebSockets for environments that need a reliable transport. The LwM2M device management standard from OMA SpecWorks is also built on CoAP.

CoAP vs MQTT

  • Model: CoAP is request/response between a client and a server, with Observe for updates; MQTT is publish/subscribe through a broker.
  • Transport: CoAP runs over UDP (and over TCP with RFC 8323); MQTT needs an ordered, lossless connection, normally TCP.
  • Reliability: CoAP retransmits confirmable messages; MQTT uses QoS levels 0, 1 and 2 on each hop.
  • Security: CoAP uses DTLS (coaps, port 5684); MQTT uses TLS, usually on port 8883.

CoAP fits constrained nodes on low-power, lossy networks, and RFC 7252 names smart energy and building automation as example applications. MQTT fits cases where many devices feed many applications through a central broker. Our comparison of MQTT, CoAP and HTTP goes into more detail.

Frequently asked questions

What is CoAP used for?+

For communication with constrained devices on low-power networks. RFC 7252 cites smart energy and building automation, and the LwM2M standard from OMA SpecWorks uses CoAP for device management.

What is the difference between CoAP and MQTT?+

CoAP follows the request/response model of HTTP over UDP, with no broker in between. MQTT is publish/subscribe over TCP, with a broker that distributes the messages. CoAP suits very constrained nodes; MQTT suits many producers and consumers sharing data.

Does CoAP use UDP or TCP?+

UDP by default, on port 5683 (5684 for coaps, secured with DTLS). RFC 8323 adds CoAP over TCP, TLS and WebSockets.

Related terms

Related articles