Unit-4
Unit-4
methodology, where clients receive information through a broker only for the topics they
are subscribed to. The broker acts as a mediator, categorizing messages into labels before
devices with low bandwidth and low power requirements. MQTT is widely used in IoT
reliable, effective path for communication. Suitable for connecting devices with a small
code footprint, MQTT is a good choice for wireless networks that experience varying
telecommunications.
Unlike the direct communication of the client-server model, the pub/sub model
decouples the client that sends messages (publisher) from the clients that receive them
subscribers.
61 | P a g e
MQTT clients can function as both publishers and subscribers, meaning a single MQTT
client can both send and receive messages. When a device (or client) sends data to a
server (or broker), it is called publishing. Conversely, receiving data from the broker is
called subscribing. Multiple clients can connect to a broker and subscribe to topics of
interest.
and delivers them once the client is back online. If a publishing client disconnects
unexpectedly, the broker can close the connection and send subscribers a cached
a. Publish: Clients send messages to the broker on a specific topic. The broker then
b. Subscribe: Clients register their interest in one or more topics with the broker. They
c. Unsubscribe: Clients can cancel their subscription to topics, stopping further message
These methods allow for a decoupled, efficient communication model suitable for IoT
An MQTT broker acts as a go-between for the clients who are sending messages and the
subscribers who are receiving those messages. In a post office analogy, the broker is the
post office itself. All messages have to go through the broker before they can be delivered
to the subscriber.
62 | P a g e
Brokers may have to handle millions of concurrently connected MQTT clients, so when
choosing an MQTT broker, enterprises should rate them based on their scalability,
MQTT provides three levels of Quality of Service (QoS) to ensure message delivery
reliability:
a. QoS 0 (At most once): Messages are delivered according to the best effort of the
b. QoS 1 (At least once): The message is guaranteed to arrive at least once. The sender
stores the message until it receives an acknowledgment (PUBACK) from the receiver.
c. QoS 2 (Exactly once): The highest level of service. It ensures the message is received
only once by using a four-step handshake process between the sender and receiver,
preventing duplicates.
QoS refers to an agreement between the sender of a message and the message's recipient.
It acts as a key feature in MQTT, giving the client the ability to choose between three
levels of service.
The three different QoS levels determine how the content is managed by the MQTT
protocol. Although higher levels of QoS are more reliable, they have more latency and
bandwidth requirements, so subscribing clients can specify the highest QoS level they
63 | P a g e
1.4.1.1. QoS 0: At most Once –
When message is published with QoS level 0, the message will be delivered at most once.
In other words, there is no guarantee that the message will be delivered or not. If
delivered, then it will be delivered maximum once. In this QoS level, the sender publishes
the message only once and then discards the message. There is no waiting for
acknowledgments in this scenario. Hence if the network goes down, the message might
not be delivered. If the network is stable the message will be delivered but only once.
When to use?
While developing applications that can afford the loss of few messages.
While developing applications that have reliable underlying network so that the
When message is published with QoS level 1, the message will be delivered at least once.
In this QoS level, the sender publishes the message and also stores it until an
64 | P a g e
acknowledgment is received from the receiver. Once an acknowledgment is received, the
sender will discard the message. If the acknowledgment is not received in time, the
message will be sent again. Hence in this QoS level, the message can be delivered, once
and sometimes even more than once, to ensure its successful delivery.
When to use?
While developing applications that can handle the delivery of duplicate messages.
While developing applications that do not have very reliable underlying network and
When message is published by the client with QoS level 2, the message will be delivered
exactly once. In this QoS level, the sender publishes the message, stores it, and waits for
PUBREC. The receiver after receiving the message sends PUBREC to the sender.
PUBREC is an indication that the message has been successfully delivered to the receiver.
Hence after receiving PUBREC, the message will be discarded by the sender. It then sends
PUBREL to the receiver. On receiving PUBREL, the receiver discards the saved states
and sends PUBCOMP. When the sender receives the final PUBCOMP, it will also discard
the previously saved states. Hence in QoS level 2, the additional messages used for
65 | P a g e
When to use?
While developing applications that require every message to be delivered exactly once.
While developing applications that can afford the additional communication cost.
While developing applications that do not have very reliable underlying network and
Slower Transmit Cycles: MQTT has slower transmit cycles compared to the
Security/Secure Sockets Layer) for security encryption. This increases the complexity
complexity of managing large topic trees and the lack of a clear method to divide these
66 | P a g e
Authentication: MQTT has minimal built-in authentication features. Usernames and
passwords are sent in cleartext, requiring SSL/TLS for secure use, which is not
Security Issues: MQTT's topic structure can form extensive trees without clear
built-in sender identification in messages, harmful messages can be easily injected into
the network. Security measures added on top of MQTT increase the code footprint and
complicate implementations.
where CoAP packets are exchanged between client nodes under the command of a CoAP
server. Unlike HTTP, CoAP is optimized for low overhead and supports state transfer
HTTP, using URIs and methods like GET, POST, PUT, and DELETE.
exchanges, which is beneficial for devices that are not always on or available.
67 | P a g e
4. UDP-based: It uses UDP as its transport protocol, which is lighter than TCP and
multiple devices.
There are two different layers that make CoAP protocol: Messages and Request/Response.
The Messages layer deals with UDP and with asynchronous messages. The
messages.
68 | P a g e
This is the lowest layer of CoAP. This layer deals with UDP exchanging messages between
endpoints. Each CoAP message has a unique id, this is useful to detect message
a binary header
a compact option
payload
Confirmable message
Non-confirmable message
a. Confirmable Message
endpoints, these messages can be reliable. In CoAP a reliable message is obtained using
a Confirmable message (CON). Using this kind of message, the client can be sure that the
message will arrive at the server. A Confirmable message is sent again and again until
the other party sends an acknowledge message (ACK). The ACK message contains the
69 | P a g e
If the server has troubles managing the incoming request it can send back a Rest message
b. Non-Confirmable Message
The other message category is the Non-confirmable (NON) messages. These are messages
that don’t require an Acknowledge by the server. They are unreliable messages or in other
words messages that do not contain critical information that must be delivered to the
server. To this category belongs messages that contain values read from sensors.
The CoAP Request/Response is the second layer in the CoAP abstraction layer. The
70 | P a g e
are several scenarios depending on if the server can answer immediately to the client
If the server can answer immediately to the client request then if the request is carried
using a Confirmable message (CON) then the server sends back to the client an
As you can notice in the CoAP message there is a Token. The Token is different from the
If the server can’t answer to the request coming from the client immediately, then it sends
then the server sends a new Confirmable message to the client containing the response.
71 | P a g e
If the request coming from the client is carried using a NON-confirmable message then
CoAP's blockwise transfer mechanism is crucial for handling large payloads that exceed
the maximum size of a single CoAP message. This mechanism divides large messages into
smaller blocks that can be transmitted individually, ensuring efficient and reliable
Significance:
ensuring that the entire payload is reliably transferred even in lossy networks.
3. Flow control: The client and server can negotiate block sizes and manage flow
control, optimizing the transfer according to the network conditions and device
capabilities.
4. Efficiency: By using smaller blocks, CoAP reduces the likelihood of message loss
responses between HTTP and CoAP. This allows CoAP devices to communicate with
1. Forward Proxy: Receives HTTP requests and forwards them as CoAP requests to the
72 | P a g e
2. Reverse Proxy: Receives CoAP requests from CoAP clients and forwards them as
HTTP requests to an HTTP server, then translates the HTTP response back to CoAP.
In this diagram:
The proxy translates this request into a CoAP request and sends it to the IoT
device.
The IoT device processes the CoAP request and sends a CoAP response back to the
proxy.
The proxy translates the CoAP response into an HTTP response and sends it back
1. Request Initialization:
An HTTP client sends a request to the proxy server. This could be a typical web
The request includes a method (GET, POST, etc.), a URI, headers, and possibly a
payload.
73 | P a g e
2. Proxy Server Handling:
The HTTP/CoAP proxy server receives the HTTP request. It understands both HTTP
The proxy server translates the HTTP request into a CoAP request. This involves
mapping the HTTP method to the CoAP method, converting headers, and possibly
The proxy server sends the translated CoAP request to the target IoT device. CoAP
uses UDP by default, making it suitable for constrained environments with limited
The IoT device receives the CoAP request, processes it, and prepares a CoAP
response.
4. Response Handling:
The IoT device sends the CoAP response back to the proxy server.
The proxy server translates the CoAP response into an HTTP response. This involves
mapping the CoAP response code to an HTTP status code, converting headers, and
5. Response Delivery:
The proxy server sends the HTTP response back to the original HTTP client.
The client receives the HTTP response and processes it as if it was communicating
74 | P a g e
Benefits include:
communicate seamlessly.
Header size It has 4 bytes sized header It has 2 bytes sized header
75 | P a g e
2.6. Differences Between AMQP and HTTP
76 | P a g e