An Iot Oriented Comparison: Rest and MQTT Rest and MQTT
An Iot Oriented Comparison: Rest and MQTT Rest and MQTT
REST vs MQTT 1
REST
Basic concepts
Basic programming
REST vs MQTT 12
Data Representation {“value": 237} vs. <value>237</value>
REST vs MQTT 14
Verbs
HTTP GET
HTTP POST
HTTP PUT
HTTP DELETE
REST vs MQTT 20
HTTP GET
How clients ask for the information they seek.
Issuing a GET request transfers the data from the server to the client
in some representation (JSON, XML, …)
GET http://www.library.edu/books
Retrieve all books
GET http://www.library.edu/books/ISBN-0011021
Retrieve book identified with ISBN-0011021
GET http://www.library.edu/books/ISBN-0011021/authors
Retrieve authors for book identified with ISBN-0011021
REST vs MQTT 21
HTTP PUT, HTTP POST
HTTP POST creates a resource
HTTP PUT updates a resource
POST http://www.library.edu/books/
Content: {title, authors[], …}
Creates a new book with given properties
PUT http://www.library.edu/books/isbn-111
Content: {isbn, title, authors[], …}
Updates book identified by isbn-111 with submitted properties
REST vs MQTT 22
REST for devices control communication
PUT /control/onoff
PUT /control/color
#00FF00
REST vs MQTT 24
HTTP DELETE
DELETE http://www.library.edu/books/ISBN-0011
Delete book identified by ISBN-0011
REST vs MQTT 25
REST is widely used
Google Maps:
https://developers.google.com/maps/web-services/
Try: http://maps.googleapis.com/maps/api/geocode/json?address=bangkok
Twitter:
https://dev.twitter.com/rest/public
Facebook:
https://developers.facebook.com/docs/atlas-apis
Amazon offers several REST services, e.g., for their S3 storage solution
http://docs.aws.amazon.com/AmazonS3/latest/API/Welcome.html
Also:
The Google Glass API, known as "Mirror API", is a pure REST API.
Here is (https://youtu.be/JpWmGX55a40) a video talk about this API. (The actual API
discussion starts after 16 minutes or so.)
Tesla Model S uses an (undocumented) REST API between the car systems and its
Android/iOS apps.
http://docs.timdorr.apiary.io/#reference/vehicles/state-and-settings
REST vs MQTT 41
Very widely…
https://apigee.com/providers
https://apigee.com/resources/instagram
REST vs MQTT 42
MQTT
Basic concepts
Basic programming
Source: https://zoetrope.io/tech-blog/brief-practical-introduction-mqtt-protocol-and-its-application-iot
REST vs MQTT 43
Message Queuing Telemetry Transport
A lightweight publish-subscribe protocol that runs on embedded
devices and mobile platforms designed to connect the physical world
devices with applications and middleware.
http://mqtt.org/
the MQTT community wiki: https://github.com/mqtt/mqtt.github.io/wiki
http://www.hivemq.com/mqtt-essentials/
Designed to provide a low latency two-way communication channel
and efficient distribution to one or many receivers.
Minimizes the amount of bytes flowing over the wire
Low power usage.
REST vs MQTT 44
Message Queuing Telemetry Transport
REST vs MQTT 45
Publish/subscribe pattern
Pub/Sub decouples a client, who is sending a particular message
(called publisher) from another client (or more clients), who is
receiving the message (called subscriber). This means that the
publisher and subscriber don’t know about the existence of one
another.
There is a third component, called broker, which is known by both the
publisher and subscriber, which filters all incoming messages and
distributes them accordingly.
Publisher 1 Publisher 2
Software bus
REST vs MQTT 50
Topics
Topic subscriptions can have wildcards
‘+’ matches anything at a given tree level so the topic “sensor/+/temp” would
match “sensor/dev1/temp”, “sensor/dev2/temp”, etc.
‘#’ matches a whole sub-tree, so “sensor/#” would match all topics under
“sensor/”.
These enable nodes to subscribe to groups of topics that don’t exist
yet, allowing greater flexibility in the network’s messaging structure.
REST vs MQTT 52
Quality of Service (QoS)
Messages are published with a Quality of Service (QoS) level, which
specifies delivery requirements.
A QoS-0 (“at most once”) message is fire-and-forget.
For example, a notification from a doorbell may only matter when immediately
delivered.
With QoS-1 (“at least once”), the broker stores messages on disk and
retries until clients have acknowledged their delivery.
(Possibly with duplicates.) It’s usually worth ensuring error messages are
delivered, even with a delay.
QoS-2 (“exactly once”) messages have a second acknowledgement
round-trip, to ensure that non-idempotent messages can be delivered
exactly once.
REST vs MQTT 54
“Will” message
When clients connect, they can specify an optional “will” message, to
be delivered if they are unexpectedly disconnected from the network.
(In the absence of other activity, a 2-byte ping message is sent to clients at a
configurable interval.)
This “last will and testament” can be used to notify other parts of the
system that a node has gone down.
REST vs MQTT 58
MQTT
Basic concepts
Basic programming
REST vs MQTT 62
Software available
Brokers (https://github.com/mqtt/mqtt.github.io/wiki/servers):
http://mosquitto.org/
http://www.hivemq.com/
https://www.rabbitmq.com/mqtt.html
http://activemq.apache.org/mqtt.html
https://github.com/mcollina/mosca
Clients
http://www.eclipse.org/paho/
Source: https://github.com/eclipse/paho.mqtt.python
http://www.hivemq.com/demos/websocket-client/
Tools
https://github.com/mqtt/mqtt.github.io/wiki/tools
REST vs MQTT 63
The suggested working environment
Installing the Python client
sudo pip install paho-mqtt
or
git clone https://github.com/eclipse/paho.mqtt.python.git
cd org.eclipse.paho.mqtt.python.git
python setup.py install
REST vs MQTT 64
Controlling the broker
Starting / stopping the broker
sudo /etc/init.d/mosquitto stop
sudo /etc/init.d/mosquitto start
man page:
https://mosquitto.org/man/mosquitto-8.html
REST vs MQTT 65
Sandboxes
https://iot.eclipse.org/getting-started#sandboxes
hostname iot.eclipse.org and port 1883.
encrypted port 8883
http://test.mosquitto.org/
http://www.hivemq.com/try-out/
http://www.mqtt-dashboard.com/
REST vs MQTT 66
Excercise
REST vs MQTT 67
Excercise
Broker information:
REST vs MQTT 68
Excercise
1. Start with the MQTT example. It will send a text message via MQTT
to the broker. Does it show up?
REST vs MQTT 69