Open In App

Constrained Application Protocol (CoAP)

Last Updated : 26 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Constrained Application Protocol (CoAP) is a special web transfer protocol that operates with constrained nodes and networks. These networks are commonly used for applications such as the Internet of Things (IoT), where devices may have limited processor, memory, and power resources. CoAP is intended to be simple, low-overhead, and efficient, making it ideal for machine-to-machine communication.

What is CoAP?

CoAP or Constrained Application Protocol, as the name suggests, is an application layer protocol that was introduced by the Internet Engineering Task Force in the year 2014. CoAP is designed for the constrained environment. It is a web-based protocol that resembles HTTP. It is also based on the request-response model. Based on the REST-style architecture, this protocol considers the various objects in the network as resources. These resources are uniquely assigned a URI or Uniform Resource Identifier. The data from one resource to another resource is transferred in the form of CoAP message packets whose format is briefly described later. The Client requests for some resources and in response to that, the server sends some response over which the client sends an acknowledgement. Although, some types of CoAP do not involve the receiver sending acknowledgments for the information received.

How Does CoAP Works?

Constrained Application Protocol (CoAP) is an application layer protocol designed for resource-constrained devices and networks, particularly in the context of the Internet of Things (IoT).

  • Client-Server Model: CoAP model is essentially a client/server model enabling the client to request for service from server as needed and the server responds to client's request.
  • Resource-Oriented: CoAP treats various objects in the network as resources, each uniquely identified by a URI (Uniform Resource Identifier). Clients can request information about these resources, and servers provide responses2.
  • Methods: CoAP supports several methods similar to HTTP:
    • GET
    • POST
    • DELETE
    • PUT
  • Asynchronous Messaging: CoAP messages are asynchronous because it uses the User Datagram Protocol (UDP). Unlike TCP-based protocols, CoAP does not require acknowledgments for every message, which helps conserve energy in resource-constrained devices.
  • Energy Efficiency: CoAP is designed to minimize energy consumption while simplifying communication between clients and devices. It achieves this by managing resources, providing device descriptions, and supporting mechanisms to determine if a device is powered on or off.

Methods in CoAP

  • GET - The get method is used to retrieve resource information identified by the request URI. In response to GET method success a 200(OK) response is sent.
  • POST - The post method creates a new subordinate resource under the parent URI requested by it to the server. On successful resource creation on the server, a 201 (Created) response is sent while on failure a 200 (OK) response code is sent.
  • DELETE - The delete method deletes the resource identified by the requested URI and a 200 (OK) response code is sent on successful operation.
  • PUT - The PUT method updates or creates the resource identified by the request URI with the enclosed message body. The message body is considered as modified version of a resource if it already exists at the specified URI otherwise a new resource with that URI is created. A 200 (OK) response is received in former case whereas a 201 (Created) response is received in later case. If the resource is neither created nor modified then an error response code is sent.

The most fundamental difference between CoAP and HTTP is that CoAP defines a new method which is not present in HTTP. This method is called Observe method. The observe method is very similar to the GET method in addition with an observe option. This alerts the server, to send every update about the resource to the client. Therefore, upon any change in the resource, the server sends a response to the client. These responses could either be directly sent individually or they can be piggy-backed.

Message Format of CoAP

CoAP messages are encoded in binary-format or 0/1 format. Like other message formats, CoAP message has a header and a payload section along with an optional section. The size of CoAP header is 4 bytes or 32 bits. This size is fixed for every CoAP message. Whereas the other part of message is the optional part which includes payload and tokens of variable size ranging from 0-8 bytes. The message format of CoAP contains the following fields:

  • Version - The size of version field is 2 bits. It represents the version of the CoAP protocol.
  • Type Code - The size of type field is 2 bits. There are four types of messages namely confirmable, non-confirmable, acknowledgement and reset represented by the bit patterns 00, 01, 10, 11 respectively.
  • Option Count - The size of option count field is 4 bits. These 4 bits, means there could be a total of 16 possible options in header.
  • Code - The size of code field is 8 bits. This indicates whether message is empty, request message or response message.
  • Message ID - The size of message ID field is 16 bits. It is used to detect the message duplication and types of messages.
  • Tokens [Optional] - The size of tokens field is variable which ranges from 0-8 bytes. It's used to match a response with request.
  • Options [Optional] - The options field in CoAP message has a variable size. It defines the type of payload message.
  • Payload [Optional] - Similar to options field, the payload field has a variable size. The payload of requests or of responses is typically a representation of the requested resource or the result of the requested action.
CoAP
CoAP Message Format

CoAP Features

Applications of CoAP

  • Real Time Monitoring in Grid - Smart cities can monitor the distribution and generation of power remotely. The CoAP sensors could be embedded inside the transformers and the data could be transferred over GPRS or 6LowPAN.
  • Defense utilities - The armory and tanks are now-a-days fitted with sensors so that information could be communicated remotely without any interference. The CoAP sensors could detect any intrusion. This makes them capable to transfer more data even under low bandwidth network.
  • Aircraft utilities - The Aircraft sensors and actuators could be connected with other sensors and communication can take place using smart CoAP based sensors and actuators.

The difference between MQTT and CoAP protocols could be understood from the following article on GFG: Difference between CoAP and MQTT

Conclusion

CoAP is a versatile and efficient protocol developed to meet the specific needs of restricted devices and networks. Due to less overhead and capability to transfer data effectively even under low bandwidth, CoAP has became a primarily choice for IoT and cloud computing system architecture. CoAP messages have a fixed size header which is of 4 bytes. Alongside this, CoAP message format have some optional fields which includes fields like token, options, and payload. This message format of CoAP is binary encoded in 0/1 format. CoAP works on request response model which majorly resembles HTTP methods like GET, POST, PUT, and DELETE along with its own unique method Observe


Next Article

Similar Reads