INTRODUCTION TO CoAP and MQTT
CoAP and MQTT are two of the most important and most used IoT protocols nowadays. Both are equally important in machine communication. CoAP is a considerable competitor for MQTT because of the similarities in their use. Yet both have advantages and disadvantages. Before we compare them, let us first have a brief introduction of both.
WHAT IS CoAP?
CoAP or Constrained Application Protocol is a server protocol for a recipient. It allows low-power small devices to operate in the internet of things(IoT).
Its architecture allows devices to connect to the IoT. Even by using networks with limited bandwidth and availability. In addition, this protocol works for machine-to-machine applications. Moreover, its design makes it good for IoT systems based on HTTP protocols, i.e., the request/response model.
WHAT IS MQTT?
MQTT or Message Queuing Telemetry Transport is a communication-based protocol used for IoT devices. In MQTT, brokers control a publish/subscribe protocol that allows one-to-many communication.
Every sensor is a client and, the server is a broker. The broker provides information to the client for which the client subscribed. Additionally, Clients can publish messages to a broker. Furthermore, they can also subscribe to a topic to receive the messages they want.
COMPARISION
As our introduction is complete, so let’s jump into their comparison based on their different features.
Basic Protocol
CoAP uses UDP or User Datagram Protocol as the transport protocol. UDP uses connectionless datagrams that make it suitable for broadcasts. Its lack of end-to-end communication makes it great for fast connections between applications.
In contrast, MQTT uses TCP or Transmission Control Protocol as its transport layer. TCP can connect different networking devices. It is safe, organized, and error-checked. Furthermore, TCP helps avoid the problems arising from packet-based messaging. Such as lost packets, duplicate packets, and corrupted packets.
Even though UDP is faster and, connectionless but TCP is more secure, reliable, and provides guaranteed transmission.
Communication Node
CoAP uses of 1:1 communication model, which means one device can communicate to only one device at a time.
MQTT, on the other hand, makes use of the M: N communication model. It ensures that a vast number of devices will interact with each other at the same time.
Communication Type
CoAP uses request-response architecture for communication between client and server. The client allows a user to request services of the server and displays the results the server returns.
However, MQTT works on publish-subscribe architecture mediated by a broker. A client publishes a topic to distribute information. Any other client can subscribe to this topic to receive this information through the broker.
Power Consumption
CoAP consumes less power as it uses UDP. The connectionless datagrams of UDP allow faster wake-up and transmit cycles to take place. Also, it contains smaller packets with less overhead. These traits enable the device to be in a sleepy state for an extended duration, saving battery power.
TCP in MQTT grants control to communicating nodes to hold their TCP sockets open for each other continuously. Consequently, it requires more power to operate.
RESTful Based
CoAP uses the RESTful principle while MQTT doesn’t.
Being RESTful allows a protocol to take advantage of existing protocols. Although any protocol can virtually use REST, it uses HTTP in conjunction for developing Web APIs. To use a REST API architecture there isn’t any need to install libraries or additional applications.
Number of Messages
CoAP supports four different types of messages: Confirmable, Non-Confirmable, Acknowledgement, Reset. The messages provide a track for exchanged information between client and server.
In contrast, MQTT uses a total of fourteen messages to establish connections between client and broker. These messages serve to connect and disconnect a client from a broker, publish data, among many other functions.
Among them are CONNECT, PUBLISH, PUBACK, SUBSCRIBE, UNSUBSCRIBE, and many others.
Header Size
In CoAP, the header size is 4 bytes.
- Ver indicates the version(2-bit).
- T: indicates message type(2-bit).
- TKL: indicates the token length(4-bit).
- Code: indicates the code response(8-bit).
- Message-ID: indicates Message ID(16-bit)
Whereas in MQTT, the header size is 2 bytes.
It has a :
2 byte fixed header (always present)
Variable-header (not always present)
Payload (not always present).
Messaging Mode
CoAP uses both Asynchronous and Synchronous Messaging Modes. This means not only it has a request/response model of communication. Wherein the application submits a request and waits until the response is returned from the device. But it also has an observe mechanism in which one node can observe others for specific transmission.
On the contrary, MQTT only supports Asynchronous mode. Which makes the use of publish/subscribe architecture. In it, the MQTT broker holds the messages and transfers them to relevant nodes when change occurs.
Security
CoAP uses DTLS or Datagram Transport Layer Security. A secure data transfer protocol that is used for datagram protocols like UDP. Therefore it can work for data that can be out of sequence or lost. Moreover, It provides privacy and prevents the risk of tampering or eavesdropping.
On the other hand, MQTT uses TLS or Transport Layer Security. Its design makes it work over a reliable transport channel like TCP. Unlike DTLS can not work if data is unsequenced or lost data. However, it provides equivalent security guarantees as DTLS.
Both of these protocols deliver data communication security over a computer network. They are widely used in email, web, VoIP, and other messaging applications. DTSL has basically emerged from TSL in order to make it useful for datagram protocol.
Reliability
The CoAP protocol makes it very easy to send a “confirmable” and a “non-confirmable” message. The receiver acknowledges the confirmable message with an acknowledgment message (ACK). This confirms that the message is received, but not that its contents have been decoded correctly or at all.
In MQTT, QoS(Quality of Service) levels 0, 1, and 2 describe growing degrees of message delivery assurance.
• Level 0:
It is also referred to as “Fire and Forget”. There is no assurance that the message will arrive and this is a single transmit burst.
• Level 1:
It tries to ensure that a message is sent to the intended receiver at least once.
• Level 2:
It helps to ensure that the intended receiver reads and decodes the letter.
CONCLUSION:
CoAP and MQTT are becoming the most popular messaging protocols for the growing IoT industry. Each protocol has its range of advantages and drawbacks, as well as difficulties and tradeoffs. Which one is more suitable? That depends upon the application.