MQTT VS Coap
MQTT VS Coap
MQTT VS Coap
Abstract— Lightweight Internet protocols are increasingly interfaces sensors embedded in the smart phone such as
being used in ubiquitous environment in order to optimize the GPS, accelerometer, microphone, etc. with external Internet
resource usage of a constrained device like a smart mobile facing system. Another example is a mobile healthcare-
gateway. This paper presents a study on the various such gateway which aggregates the various sensor data such as
protocols to optimize the usage of energy, and network blood pressure, blood sugar, ECG, etc. from portable
resources, computation cost of a constrained gateway device. sensors. Sensor data is aggregated, filtered, pre-processed,
Comprehensive analysis and feature wise categorization of analyzed and then posted to the backend applications in the
existing dominant protocols, namely MQTT (message queue cloud. In resource constrained gateway energy consumption,
telemetry transport), CoAP (constrained application protocol)
CPU cycles, memory usage, networking resources and
are provided here to achieve improved understanding of the
existing issues and gaps in this domain. The present work
bandwidth usage must be used as efficiently as possible.
further identifies the best suited application areas for each In this paper, we study existing lightweight application
protocol based on the results corresponding to typical resource protocols like CoAP (Constrained Application Protocol) and
requirements and performance attributes. Finally our vision MQTT (Message Queue Telemetry Transport). They are
on the research scope is presented here mainly for optimization compared considering various features such as energy
of energy usage. consumption, bandwidth utilization, reliability, etc. Based on
that analysis, these protocols are mapped to the best suited
Keywords-constrained device; ubiquitous computing; application areas. As CoAP supports both request-response
protocol;lightweight and resource-observe (a variant of pub-sub), this paper
illustrates how the adaptive feature of CoAP for request-
I. INTRODUCTION response and publish-subscribe mode, can be leveraged with
a common interface using sequence diagram and sate
The Internet of Things ( IoT) consisting of ubiquitous machine. Further, existing issues and gaps, and future
smart devices and objects with embedded sensors requires research scopes are analyzed and discussed based on the
energy and bandwidth efficient interfacing of resource- above review.
constrained devices with other devices, backend applications,
servers and other cloud hosted systems. Interoperation with a
wide variety of sensors and smart devices, semantic
interoperability, collection and analysis of sensor data,
context detection, predictive analytics and generation of
control and actuation outputs are some of the important
aspects of IoT applications. IoT applications require access
to sensor data in real-time as per their needs. The gateway
device is one of the most important components required for
realization of the IoT. It connects the sensor / device domain
to backend application/web service domain. In this paper we
consider those gateways that are low cost battery powered
embedded systems and have limited processing power,
memory and storage. Fig. 1 depicts the generic system
overview.
The gateway dynamically aggregates the real time sensor
data and shares the sensor data with the destination
server/application over Internet and at the same time
manages and controls the various diverse sensing-
devices/sensors. An example of a resource constrained
gateway is the smart mobile phone based gateway that Figure 1. A generic architecture for connecting Sensor to the Web.
335
working philosophy. It supports three QoS (quality of
service) levels for message delivery with enhanced reliability
and has a low header overhead [2]. MQTT is a connection
oriented protocol as the publisher or the subscriber both
needs persistent connection to the message broker. It relies
on the underlying TCP layer for connection features.
Fig. 2 presents the mapping of these protocols in the OSI
stack.
Table I provides the feature wise comparison of these
protocols along with the potential application areas for CoAP
and MQTT.
Since CoAP uses a HTTP like request-response Figure 2. Mapping of protocols into the OSI stack.
architecture and resembles HTTP in many ways CoAP can
be well used for HTTP like GET, PUT, POST, DELETE
request based applications. Thus it can be used as the In this mode of operation CoAP clients called ‘observers’
protocol behind browsing like applications on a constrained register their interests about a specific resource in the
server. Potential applications may well use a proxy to namespace of a known server. The observers are notified
convert CoAP requests to HTTP, the language the Web is whenever the subject undergoes a change. The observers
more conversant with traditionally, as exemplified in [9]. register with the subject using the GET request with a
MQTT, by virtue of its inherent publish-subscribe special ‘observe’ option activated. The subject puts the
architecture, is very useful for ‘PUSH’ like application so as observer in the list of observers if it is allowed and responds
to free the constrained devices from resource hungry to the observer with an immediate state of the resource.
operations like ‘polling’ to get the updated data [5]. After the initial response each subsequent notification is an
However, in case of constrained edge gateways this protocol additional CoAP response sent by the server in reply to the
can be used for ‘publishing’ real time aggregated sensor data GET request and includes a complete representation of the
to the web-server asynchronously and with reliability. Efforts new resource state. Fig. 3 depicts an example scenario with
have been made to integrate MQTT [12] with ‘Pachube’, a 3 notifications.
widely popular technology for monitoring sensors and The resource observing model can be easily extended to
devices using ‘polling’, such that ‘Pachube’ can take publish/subscribe data exchange with a publisher client,
advantage of the MQTT broker by configuring triggers and subscriber clients and a broker in between utilizing PUT and
can be implemented using HTTP POST. ‘observable’ GET request from two independent clients.
However, CoAP seems to have a wider acceptance for Fig. 4 depicts the handshake in this case.
constrained devices due to ease of integration with
6LowPAN, easy portability with HTTP, UDP based
operation with low connection overhead, support for several
features like sleeping nodes [3] (without requiring persistent
connection), support for both request-response and resource-
observe mode, etc. Also, apart from the IETF, CoAP is
backed up by ETSI [22] and is poised for wide industry
adaptation as evident from the participation list in the recent
ETSI plug test of CoAP [18].
IV. COAP AS A PUBLISH/SUBSCRIBE PROTOCOL
The request/response model does not work well for
clients which are interested in having a current
representation of a resource/ sensor over a period of time.
Conventional method like polling also proves inefficient in
terms of resource usage for constrained devices. Thus, in
scenarios where real-time updates from the constrained
clients are required, MQTT may seem as an acceptable
solution. However, realizing the need for such real-time
feature of a proposed protocol, IETF extends the CoAP core
protocol with a mechanism to push resource representations
from servers to interested clients, while still keeping the
properties of REST [16].
Figure 3. An example resource observing exchange (from [16]).
336
TABLE I.
Transport Supported Payload Response Max re- Multicast Reliability Typical application
layer architecture & header timeout transmit support area
structure
Mainly Publish- 2 bytes Configurable. Configurable One-to-many With 3 QoS Topic based real-time messaging
TCP [2]. subscribe header. Timeout message levels kind of application using pub/sub
Payload value distribution is requiring persistent connection
MQTT
Mainly REST 4 byte Default 4[3]. ‘Resource Simple stop- Applications with sensors
UDP, TCP based header 2 seconds 0 for observe’ mode and-wait (possibly with defined sleeping
can be request / +Type [3]. multicast can be used for retransmission cycle) requiring to run RESTful
used but response, Length Can be one-to-many reliability web-services to achieve direct
not usually Resource Value configured. messaging. with connectivity to the Web (or
[3]. observe (TLV). Dedicated exponential optionally to some G/W) with
(Publish- Supports multicast back-off HTTP like methods and URI
subscribe) payload support exists [21] using either or both
CoAP
The discussion in the above section brings out an Current section performs an analysis of resource
advantage of CoAP. The configuration of the GET request requirements for CoAP and MQTT protocols based on
determines whether a client-server pair would interact in experimental results.
request-response or in resource-observe mode. Thus, The experiments were conducted using Intel X86 systems
without making any changes in the infrastructure, the same with Ubuntu installation as the nodes. Smart phone with
system can act both as a request/response and
337
Android O/S is planned as our future research platform. implementation for CoAP and MQTT respectively. It is
Fig. 6(a) depicts the experimental set up. worth mentioning here that the ‘libcoap’ distribution
supports IPv6. However, the modems used in the
experiment support only IPv4. So, the ‘libcoap’ code was
changed to support IPv4. This brings parity with the
‘mosquitto’ implementation which used IPv4 at the network
layer.
Different sample sensor data of constant size were used
as payload for both the protocols. Performance of the
protocols were studied by changing the packet loss
condition for a given payload size.
Fig. 7 and 9 shows the comparison between MQTT and
CoAP based on the actual network data transferred for
transferring different size of sensor payload with 0% packet
loss with CoAP in request-response mode and resource-
observe mode respectively. Fig. 8 and 10 shows the
comparison for 20% packet loss using CoAP in request-
response and resource-observe mode respectively.
It is obvious from the graphs that MQTT consumes
higher bandwidth than CoAP for transferring same payload
under same network condition. The MQTT traffic was
captured while MQTT publisher publishes data to the
broker. Since MQTT runs on TCP, it has TCP connection
overhead for connection establishment and closing. The
publisher client establishes a connection with the broker
first, followed by a ‘PUBLISH’ from the publisher. In case
of QoS 0 the publisher expects no more application layer
feedback from the broker and the TCP connection closes
with normal transport layer exchange of ACKs. But, in case
of QoS 2 the publisher performs additional exchanges with
the broker to ensure ‘exactly once’ delivery. This causes
more traffic than QoS 0 case.
On the other hand, CoAP has no connection overhead as
UDP works in ‘fire and forget’ basis. To ensure reliable
transmission, the CoAP messages were CON (confirmable)
messages with an application layer ACK message. But since
no connection was established there is no question of
overhead to close the connection. It has been observed that
as packet loss increases, total number bytes transferred also
increases because of retransmission.
It is important to note that in case of MQTT for packet
loss scenarios like Fig 8. and Fig. 10, when the actual bytes
sent is increased, the total bytes of data transferred dropped
Figure 5. a) Server side flow showing how a GET request can be because of an early connection closure during TCP
served both as request response (the dotted lines) and resource-
observe mode. b) The state m/c for the client for an ‘observe GET’ connection establishment or publisher broker handshaking
request. phase.
The data overhead analysis exposes the fact that topic
definition is proving to be an extra overhead in case of
The connection between the nodes was routed through
MQTT as the topic string itself is part of the payload.
an intermediate router known as Wide Area Network However, efficient formation of topic string using wild
Emulator (WANEM) [17] in order to control the network cards [11] may reduce the topic overhead for MQTT. On the
attributes for the experiment. WANEM allows controlling other hand, URI for locating resources forms a significant
the physical network properties like bandwidth, packet loss,
part of the CoAP packet [3] and an optimized URI path is
etc. to simulate constrained network, as shown in Fig. 6(b).
needed for reducing overhead.
Wireshark [20] tool was used to analyze the network traffic.
In terms of energy both the protocols are efficient. Detail
We used ‘libcoap’ [12] and ‘mosquitto’ [13] as the energy consumption figures for MQTT are available in [15]
338
for Android implementation. Fig. 11 shows the average based on our experimental data, where CoAP becomes most
energy consumed by MQTT and CoAP with reliability energy efficient.
(a) (b)
Figure 6. (a) The experimental set up using a private network for performing CoAP and MQTTperformance comparison.
(b) Simulating the different network conditions.
Figure 7. Data bytes sent for different sensor data posting with 0% Figure 9. Data bytes sent for different sensor data posting with 0%
packet loss with CoAP in request-response packet loss with CoAP in resource-observe
Figure 8. Data bytes sent for different sensor data posting with 20% Figure 10. Data bytes sent for different sensor data posting with
packet loss with CoAP in request-response 20% packet loss with CoAP in resource-observe
339
association with over all QoS in a more efficient way,
where UDP is considered as basic transport layer protocol.
The protocols possess the low overhead for header
parsing; however optimized encoding for payload
compression is a further challenge to be resolved.
Preservation of privacy while carrying the sensor data
including the context information is also another open area
to be addressed. Currently we are pursuing our research
activity on the above mentioned open issues.
REFERENCES
340