Professional Documents
Culture Documents
CH 4 Edge To Cloud Protocol
CH 4 Edge To Cloud Protocol
Protocols
Dr. Ashish Vanmali
Outline of the Chapter
HTTP request
Web
Web
client
server
(browser)
HTTP response
(content)
❖Advantages to HTTP:
▪ Reliability: Message delivery is guaranteed and acknowledged.
▪ Ubiquity: HTTP is used all over the place and is very easy to implement.
▪ Ease of implementation: If you can connect to the internet, you can use HTTP
anywhere in the world. No special hardware or software is required.
❖Disadvantages to HTTP:
▪ Higher power needs: Communications have frequent back and forth interactions,
connections need to be sustained, and plain text results in larger message sizes. All of
this requires more power to support.
▪ The IoT device complexity: The device needs enough memory and CPU power to
support the TCP protocol and high level HTTP RESTful APIs.
Dr. Ashish Vanmali, VCET, Vasai 9
WebSocket
▪ WebSocket is a bi-directional communication protocol which has emerged recently
with the introduction of HTML5.
▪ WebSocket protocol is not just an enhancement of the current HTTP protocol; it is
enormously advanced, especially for real-time, event-driven communication.
▪ WebSocket is a low-latency (real-time), full-duplex (bidirectional), long-running
(persistent), single connection (TCP) between a client and server.
▪ WebSockets provide a huge benefit for real-time, event-driven web applications.
▪ It is used for real-time data updates and synchronization, live text chat, video
conferencing, VOIP, IoT control and monitoring.
▪ These capabilities enable apps in gaming, social networks, logistics, finance and
home, vehicle and industrial automation etc.
Ref:
https://www.hcltech.com/site
s/default/files/images/inline-
migration/images/html5.jpg
Dr. Ashish Vanmali, VCET, Vasai 11
WebSocket
Ref:
https://blog.scaleway.com/co
ntent/images/2021/02/webso
ckets-bigger-4.png
Dr. Ashish Vanmali, VCET, Vasai 13
WebSocket
❖Advantages to WebSocket:
▪ Bidirectional:
• HTTP relies on a client request to receive a response from the server for every
exchange.
• WebSockets allow for full-duplex bidirectional communication. This enables the
server to send real-time updates asynchronously, without requiring the client to
submit a request each time.
▪ Persistent:
• Rather than establishing and terminating the connection for each client request and
server response, WebSockets allow for a persistent client-server connection.
• After an initial HTTP “handshake,” the connection is kept alive using a “ping-pong”
process, in which the server continuously pings the client for a response.
• The server only terminates this connection after an explicit request from the client,
or implicitly when the client goes offline.
Dr. Ashish Vanmali, VCET, Vasai 14
WebSocket
❖Advantages to WebSocket:
▪ Low Latency:
• By eliminating the need for a new connection with every request, WebSockets
greatly reduce the data size of each message, drastically decreasing latency.
• After the initial handshake, which includes standard HTTP header information, all
subsequent messages include only relevant information.
▪ Extensible:
• Flexibility is ingrained into the philosophy and design of the WebSocket protocol,
enabling the implementation of subprotocols and extensions for additional
functionality.
• Currently, the WebSocket API supports over 40 subprotocols such as WAMP, XMPP,
AMQP and MQTT, as well as extensions that enable powerful functionality like
multiplexing and data compression. This makes WebSockets a highly adaptable.
Dr. Ashish Vanmali, VCET, Vasai 15
WebSocket
❖Advantages to WebSocket:
▪ Secure:
• The WebSocket Secure (WSS) protocol uses standard SSL and TLS encryption to
establish a secure connection between the client and server.
• Using a mutually agreed-upon authorization and authentication system, the client
and server can safely exchange encrypted WebSocket messages, which is critical in
sensitive government, military and corporate applications.
• While unsecured WebSockets uses TLS port number 80, WSS uses port 443.
❖Disadvantages to WebSocket:
▪ WebSocket requires a TCP implementation, which may or may not be a problem, but
it also requires an HTTP implementation for the initial connection setup.
▪ Additionally, WebSocket was not designed with the requirements of highly
constrained embedded systems in mind, so implementations may not be
straightforward.
❖CoAP Architecture:
Ref:
https://www.wallarm
.com/what/coap-
protocol-definition
Dr. Ashish Vanmali, VCET, Vasai 21
CoAP
❖CoAP Architecture:
▪ The Internet and the constraints ecosystem are the 2 foundational elements of the
CoAP protocol architecture.
▪ Here, the server monitors and helps in communication happening using CoAP and
HTTP while proxy devices bridge the existing gap for these 2 ecosystem, making the
communication smoother.
▪ CoAP allows CoAP clients exchange data/information with each other within resource
constraints.
▪ The server provides resources identified by the URIs. The client communicates with a
server with a known, predefined address.
▪ CoAP offers 7 PDU types, i.e., 4 request methods (GET, POST, PUT, DELETE) and the
response, acknowledgement, reset messages.
Dr. Ashish Vanmali, VCET, Vasai 22
CoAP
Ref: IoT
Fundamentals
Bootcamp,
Cisco 2019
Dr. Ashish Vanmali, VCET, Vasai 27
CoAP
❖CoAP Communication Example:
▪ CoAP is used to adjust the brightness of the light
Ref: IoT
Fundamentals
Bootcamp,
Cisco 2019
Dr. Ashish Vanmali, VCET, Vasai 28
CoAP
❖Advantages of CoAP:
▪ Reduced power requirements: It operates over UDP, which requires minimal
overhead for communications. It also allows faster wake up times and extended
sleepy states. Taken together, this means batteries last longer for IoT devices.
▪ Smaller packet size: Another advantage of UDP is small packet sizes. This leads to
faster communication cycles. Again, this allows batteries to last longer.
▪ Security: When Datagram Transport Layer Security (DTLS) is employed over UDP,
communication is encrypted and secure. (Note that it is optional)
▪ Asynchronous communication option: Clients can request to observe a device by
setting a flag. The server (IoT device) can then stream state changes to the client as
they happen. Either side can cancel the observe request.
▪ IPv6 based: It was designed from the beginning to support IPv6. This also allows for a
multicasting option.
▪ Resource discovery: Servers can provide a list of resources and media types. The
client can then review and discover what is available.
Dr. Ashish Vanmali, VCET, Vasai 29
CoAP
❖Disadvantages of CoAP:
▪ Message unreliability: UDP does not guarantee the delivery of datagrams. CoAP adds
a method to request a confirmation acknowledgement to confirm the message was
received. This does not verify that it was received in its entirety and decoded
properly.
▪ Standards are still maturing: CoAP is still evolving, although there is a lot of market
momentum behind it. It is likely to mature quickly as use becomes more widespread.
▪ NAT issues: Network Address Translation (NAT) devices are commonly used in
enterprise networks and cloud environments. CoAP can have problems
communicating with devices behind a NAT since the IP can be dynamic over time.
▪ Security: CoAP is unencrypted by default. This makes it natively unsecure and you
need to take additional steps to make sure communication is not open to hackers.
❖QoS in MQTT:
▪ The MQTT protocol offers three levels of quality of service (QoS).
▪ QoS for MQTT is implemented when exchanging application messages with publishers
or subscribers, and it is different from the IP QoS.
Level of QoS Description
QoS 0 • This is a best-effort and unacknowledged data service referred to as “at
most once” delivery.
• The publisher sends its message one time to a server, which transmits it
once to the subscribers.
• No response is sent by the receiver, and no retry is performed by the
sender.
• The message arrives at the receiver either once or not at all.
Dr. Ashish Vanmali, VCET, Vasai 40
MQTT
❖QoS in MQTT:
Level of QoS Description
QoS 1 • This QoS level ensures that the message delivery between the publisher
and server and then between the server and subscribers occurs at least
once.
• In PUBLISH and PUBACK packets, a packet identifier is included in the
variable header. If the message is not acknowledged by a PUBACK packet, it
is sent again.
• This level guarantees “at least once” delivery.
❖QoS in MQTT:
Level of QoS Description
QoS 2 • This is the highest QoS level, used when neither loss nor duplication of
messages is acceptable.
• There is an increased overhead associated with this QoS level because
each packet contains an optional variable header with a packet identifier.
• Confirming the receipt of a PUBLISH message requires a two-step
acknowledgement process.
• The first step is done through the PUBLISH/PUBREC packet pair, and the
second is achieved with the PUBREL/PUBCOMP packet pair.
• This level provides a “guaranteed service” known as “exactly once”
delivery, with no consideration for the number of retries as long as the
message is delivered once.
Dr. Ashish Vanmali, VCET, Vasai 42
MQTT
❖QoS in MQTT:
❖QoS in MQTT:
❖QoS in MQTT:
❖Advantages to MQTT:
▪ Packet agnostic: Any type of data can be transported in the payload carried by the
packet as long as the receiving party knows how to interpret it.
▪ Reliability: Offers Quality of Service (QoS) options that can be used to guarantee
delivery.
▪ Scalability: The publish/subscribe model scales well in a power-efficient way.
▪ Decoupled design: There are several elements to the design that decouple the device
and the subscribing server, which result in a more robust communication strategy.
▪ Time: A device can publish its data regardless of the state of the subscribing server.
The subscribing server can then connect and receive the data when it is able. This
decouples the two ends of the communication on a time basis.
❖Advantages to MQTT:
▪ Space (delivery details): It also allows both ends to operate independently. The
subscriber can be changed or upgraded and additional subscribers added with no
change needed on the publishing device end.
▪ Synchronizing: The process is asynchronous. There is no need to interrupt work in
progress to publish a message.
▪ Bidirectional: A device can be both a publisher and a subscriber. In this way, it can
receive commands by subscribing to a topic on the broker. It could also receive data
from other devices that could be an input into the data it publishes.
❖Disadvantages to MQTT:
▪ It operates over TCP: TCP requires more handshaking to set up communication links
before any messages can be exchanged. This increases wake-up and communication
times, which affects the long-term battery consumption. TCP connected devices tend
to keep sockets open for each other with a persistent session. This adds to power and
memory requirements.
▪ Centralized broker can limit scale: The broker can affect scalability as there is
additional overhead for each device connected to it. The network can only grow as
large as the local broker hub can support it. This puts a limit on expansion for each
hub and spoke group.
▪ Broker single point of failure: The failure of broker brings the entire network down.
▪ Security: MQTT is unencrypted by default. This makes it natively unsecured and
requires you to take additional steps and absorb some overhead to make sure TLS/SSL
is implemented.
Dr. Ashish Vanmali, VCET, Vasai 48
CoAP v/s MQTT
Ref:
https://www.wallarm
.com/what/coap-
protocol-definition
Dr. Ashish Vanmali, VCET, Vasai 49
CoAP v/s MQTT
CoAP MQTT
Uses UDP as main transport protocol Uses TCP as main transport protocol
Based on request – response model Based on publish – subscribe model
Message dispatching happens on a unicasting basis Central broker handles message dispatching,
(one-to-one). The process is same as HTTP. following the optimal publisher to client path.
Communication model is one-to-one Communication model is many-to-many
It uses asynchronous and synchronous messaging. It uses only asynchronous mode for messaging.
Viable for state transfer Suitable for event-oriented operations
It defines messages properly using labels and makes No message labeling but have to use diverse
its discovery easy. messages for different purposes.
Effectiveness in LLN (Low power Lossy Networks) is Effectiveness in LLN (Low power Lossy Networks) is
excellent. low.
Dr. Ashish Vanmali, VCET, Vasai 50
CoAP v/s MQTT
CoAP MQTT
Security is provided through DTLS (Datagram Security is not defined. It is provided through SSL
Transport Layer Security) (Secure Sockets Layer) / TLS (Transport Layer Security)
encryption
It has 4 bytes sized header It has 2 bytes sized header
It is RESTful based. It is not RESTful based.
It does not have persistence support. It is mainly used for live communication and has
persistence support.
Power consumption is lower than MQTT Power consumption is higher than CoAP
Reliability is provided through Confirmable messages, Provides reliability through 3 Quality of service levels
Non-confirmable messages, Acknowledgements, and viz., QoS 0: Delivery not guaranteed, QoS 1: Delivery
Retransmissions confirmation, QoS 2: Delivery double confirmation
Dr. Ashish Vanmali, VCET, Vasai 51
MQTT-SN
❖MQTT-SN Architecture:
Ref: https://www.oasis-
open.org/committees/do
wnload.php/66091/MQTT
-SN_spec_v1.2.pdf
Dr. Ashish Vanmali, VCET, Vasai 55
MQTT-SN
❖MQTT-SN Architecture:
▪ There are three kinds of MQTT-SN components, MQTT-SN clients, MQTT-SN gateways
(GW), and MQTT-SN forwarders.
▪ MQTT-SN clients connect themselves to a MQTT server via a MQTT-SN GW using the
MQTT-SN protocol. A MQTT-SN GW may or may not be integrated with a MQTT
server.
▪ In case of a stand-alone GW the MQTT protocol is used between the MQTT server and
the MQTT-SN GW. Its main function is the translation between MQTT and MQTT-SN.
▪ MQTT-SN clients can also access a GW via a forwarder in case the GW is not directly
attached to their network.
▪ The forwarder simply encapsulates the MQTT-SN frames it receives on the wireless
side and forwards them unchanged to the GW; in the opposite direction, it
decapsulates the frames it receives from the gateway and sends them to the clients,
unchanged
Dr. Ashish Vanmali, VCET, Vasai too. 56
MQTT-SN
MQTT-SN MQTT-SN
MQTT
MQTT
Broker Broker
Ref: https://www.oasis-
open.org/committees/do Clients Clients
wnload.php/66091/MQTT Transparent Aggregating
-SN_spec_v1.2.pdf GW GW
Dr. Ashish Vanmali, VCET, Vasai 57
MQTT-SN
❖STOMP Frame:
▪ Communication between Client-Server is through a “frame” consisting of a number of
lines.
▪ The first line contains the command, followed by headers in the form <key>: <value>
(one per line), followed by a blank line and then the body content, ending in a null
character.
COMMAND
header1:value1
header2:value2
Body^@
❖STOMP Frame:
▪ The protocol uses following commands:
• CONNECT
• SEND
• SUBSCRIBE
• UNSUBSCRIBE
• BEGIN
• COMMIT
• ABORT
• ACK
• NACK
• DISCONNECT
Dr. Ashish Vanmali, VCET, Vasai 62
STOMP
❖CONNECT:
▪ A STOMP client initiates the stream or TCP connection to the server by sending the
CONNECT frame.
CONNECT
accept-version:1.2
host:stomp.github.org
^@
▪ If the server accepts the connection attempt, it will respond with a CONNECTED
frame.
CONNECTED
version:1.2
^@
❖CONNECT:
▪ The server can reject any connection attempt. The server should respond back with
an ERROR frame explaining why the connection was rejected and then close the
connection.
ERROR
version:2.0
content-type:text/plain
^@
❖CONNECT:
Protocol Negotiation:
▪ The CONNECT frame must include the accept-version header. It should be set
to a comma separated list of incrementing STOMP protocol versions that the client
supports.
CONNECT
accept-version:1.0,1.1,2.0
host:stomp.github.org
^@
▪ The protocol that will be used for the rest of the session will be the highest protocol
version that both the client and server have in common.
❖CONNECT:
Protocol Negotiation:
▪ The server will respond back with the highest version of the protocol that it has in
common with the client.
CONNECTED
version:1.1
^@
▪ If the client and server do not share any common protocol versions, then the server
must respond with an ERROR frame.
ERROR
version:1.2,2.1
content-type:text/plain
^@
Dr. Ashish Vanmali, VCET, Vasai 66
STOMP
❖SEND:
▪ The SEND frame sends a message to a destination in the messaging system.
▪ It has one required header is destination , which indicates where to send the
message.
▪ The body of the SEND frame is the message to be sent.
SEND
destination:/queue/a
content-type:text/plain
hello destination
^@
❖SUBSCRIBE:
▪ The SUBSCRIBE frame is used to register to listen to a given destination.
▪ Like the SEND frame, the SUBSCRIBE frame requires a destination header
indicating the destination to which the client wants to subscribe.
▪ Any messages received on the subscribed destination will henceforth be delivered as
MESSAGE frames from the server to the client.
▪ The ack header controls the message acknowledgment mode.
▪ If the server cannot successfully create the subscription, the server must send the
client an ERROR frame and then close the connection.
SUBSCRIBE
id:0
destination:/queue/foo
ack:client
^@
Dr. Ashish Vanmali, VCET, Vasai 68
STOMP
❖SUBSCRIBE:
▪ An id header must be included in the frame to uniquely identify the subscription.
▪ Within the same connection, different subscriptions must use different subscription
identifiers.
▪ The valid values for the ack header are auto, client, or client-individual.
▪ If the header is not set, it defaults to auto. Here the client does not need to send the
server ACK frames for the messages it receives.
▪ In client, the client must send the server ACK frames for the messages it
processes.
▪ In client-individual, the acknowledgment operates just like the client
acknowledgment mode except that the ACK or NACK frames sent by the client are
not cumulative. This means that an ACK or NACK frame for a subsequent message
must not cause a previous message to get acknowledged.
▪ In case the client did not process some messages, it should send NACK frames to tell
the server
Dr. Ashish Vanmali, VCET, Vasai it did not consume these messages. 69
STOMP
❖UNSUBSCRIBE:
▪ The UNSUBSCRIBE frame is used to remove an existing subscription.
▪ Once the subscription is removed the STOMP connections will no longer receive
messages from that subscription.
▪ An id header must be included in the frame to uniquely identify the subscription to
remove. This header must match the subscription identifier of an existing
subscription.
UNSUBSCRIBE
id:0
^@
❖ACK:
▪ ACK is used to acknowledge consumption of a message from a subscription using
client or client-individual acknowledgment.
▪ The ACK frame must include an id header matching the ack header of the MESSAGE
being acknowledged.
▪ Optionally, a transaction header may be specified, indicating that the message
acknowledgment should be part of the named transaction.
ACK
id:12345
transaction:tx1
^@
❖NACK:
▪ NACK is used to tell the server that the client did not consume the message.
▪ NACK takes the same headers as ACK.
NACK
id:12345
transaction:tx1
^@
❖BEGIN:
▪ BEGIN is used to start a transaction.
▪ The transaction header is required, and the transaction identifier will be used for
SEND, COMMIT, ABORT, ACK, and NACK frames to bind them to the named
transaction.
▪ Within the same connection, different transactions must use different transaction
identifiers.
BEGIN
transaction:tx1
^@
❖COMMIT:
▪ COMMIT is used to commit a transaction in progress.
COMMIT
transaction:tx1
^@
❖ABORT:
▪ ABORT is used to roll back a transaction in progress.
ABORT
transaction:tx1
^@
❖DISCONNECT:
▪ DISCONNECT is used to stop a transaction.
▪ To ensure that the previously sent frames have been received by the server, a three
step process is followed:
1. Send a DISCONNECT frame with a receipt header set.
DISCONNECT
receipt:77
^@
^@
❖Working of AMQP:
▪ AMQP uses publish-subscriber model.
Ref:
https://support.smart
bear.com/readyapi/do
cs/testing/amqp.html
Dr. Ashish Vanmali, VCET, Vasai 77
AMQP
❖Working of AMQP:
▪ In AMQP, applications that send messages (known as publishers) to AMQP brokers
that distribute them to applications that process messages (consumers).
▪ The internal structure or an AMQP broker includes three types of entities: exchanges,
queues and bindings.
▪ Messages posted to a broker first go to an exchange.
▪ Then, the exchange will route the message to a queue that is bound to the exchange
using special rules called bindings.
▪ Each exchange can have zero or more queues bound to it.
▪ Lastly, consumers will receive the messages from the queue (they can be subscribed
to it, or the user can fetch the messages manually).
❖Message Routing:
▪ Both bindings and messages contain strings called routing keys that the broker uses
to decide where to route the message.
▪ The mode of routing messages from an exchange to a queue is based on the type of
the exchange.
▪ The routing keys of messages sent to a topic exchange must be a list of dot-separated
words. For examples:
office.server.test
office.server.update
office.client.test
❖Message Routing:
▪ There are four types of exchanges:
• Direct: Direct exchanges route messages to queues that are bound to them if the
binding routing key is identical to the message routing key.
• Fanout: Fanout exchanges route messages to all the queues that are bound to
them. The message routing key is ignored.
• Topic: Topic exchanges route messages to bound queues if the message routing key
matches the pattern specified in the binding routing key.
• Headers: Headers exchanges route messages to bound queues based on multiple
attributes expressed as message headers rather that the routing key.
Communication
It has asynchronous communication nature. It has synchronous communication nature.
Nature
Message Delivery It has guaranteed message delivery. It has no guarantee for message delivery.
AMQP protocol can bear the server broke issue HTTP protocol is not capable to react to the
Fault Tolerance
on its own. server breakdown issue.
It has the property of segmentation and can It does not has this capability to treat each
Segmentation
process messages into slots. message as segments.