際際滷

際際滷Share a Scribd company logo
MQTT
A nice way to connect 'things' in IoT
Henri Cavalcante
github.com/henricavalcante
twitter.com/henricavalcante
fb.me/henricavalcante
Message Queue Telemetry Transport is a publish-subscribe
lightweight protocol who runs over TCP protocol, it's
very fast and simple and runs pretty well over high
latencies and unreliable networks.
What is it?
NodeJS Installation
$ curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
$ sudo apt-get install -y nodejs
$ brew install node
$ curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -
$ yum -y install nodejs
Debian / Ubuntu
Red Hat / CentOS / Fedora
OSX
Windows
https://nodejs.org/en/download/
MQTT Installation
$ sudo npm install --global mqtt
ClientServer (Mosca.JS)
$ sudo npm install --global mosca bunyan
$ mosca -v
+++.+++: ,+++ +++; '+++ +++.
++.+++.++ ++.++ ++,'+ `+',++ ++,++
+` +, +: .+ .+ +; +; '+ '+ +` +`
+` +. +: ,+ `+ ++ +; '+ ;+ + +.
+` +. +: ,+ `+ +' '+ + +.
+` +. +: ,+ `+ :+. '+ +++++.
+` +. +: ,+ `+ ++ '+ +++++.
+` +. +: ,+ `+ ++ '+ + +.
+` +. +: ,+ `+ +: +: '+ ;+ + +.
+` +. +: .+ .+ +; +; '+ '+ + +.
+` +. +: ++;++ ++'++ ++'+' + +.
+` +. +: +++ +++. ,++' + +.
$ mqtt sub -h localhost -t hello -v
$ mqtt pub -h localhost -t hello -m GDGNatal
Standard for IoT
OASIS is pleased to announce that MQTT Version 3.1.1 from
the OASIS Message Queuing Telemetry Transport (MQTT) TC
has been approved by the membership as an OASIS Standard.
Publish-subscribe
Client 3Broker
Topic
Client 1
Client 2
Publish
bit 7 6 5 4 3 2 1 0
Byte 1 Message Type DUP flag QoS level RETAIN
Byte 2 Remaining Length
Lightweight
MQTT Header
$ echo "GET / HTTP/1.1" | wc -c
15 Bytes
$ curl "http://localhost:1880/teste/" -v 2>&1 | grep '>' | cut -c 3- | wc -
c
84 Bytes
HTTP Header
Message Type
Mnemonic Enum Description
Reserved 0 Reserved
CONNECT 1 Client request to connect to Server
CONNACK 2 Connect Acknowledgment
PUBLISH 3 Publish message
PUBACK 4 Publish Acknowledgment
PUBREC 5 Publish Received (assured delivery part 1)
PUBREL 6 Publish Release (assured delivery part 2)
PUBCOMP 7 Publish Complete (assured delivery part 3)
SUBSCRIBE 8 Client Subscribe request
SUBACK 9 Subscribe Acknowledgment
UNSUBSCRIBE 10 Client Unsubscribe request
UNSUBACK 11 Unsubscribe Acknowledgment
PINGREQ 12 PING Request
PINGRESP 13 PING Response
DISCONNECT 14 Client is Disconnecting
Reserved 15 Reserved
Position: byte 1, bits 7-4.
Represented as a 4-bit unsigned value.
The enumerations for this version of
the protocol are shown in the table
below.
Flags
Name Bit position Description
DUP 3 Duplicate delivery
QoS 2-1 Quality of Service
RETAIN 0 RETAIN flag
DUP Flag
This flag is set when the client or server attempts to re-deliver a PUBLISH, PUBREL,
SUBSCRIBE or UNSUBSCRIBE message. This applies to messages where the value of QoS is
greater than zero (0), and an acknowledgment is required. When the DUP bit is set,
the variable header includes a Message ID.
The recipient should treat this flag as a hint as to whether the message may have
been previously received. It should not be relied on to detect duplicates.
Name Bit position Description
DUP 3 Duplicate delivery
QoS 2-1 Quality of Service
RETAIN 0 RETAIN flag
Quality of Service Level
QOS Level Description
0 (00) At most once
1 (01) At least once
2 (10) Exactly once
Name Bit position Description
DUP 3 Duplicate delivery
QoS 2-1 Quality of Service
RETAIN 0 RETAIN flag
Quality of Service Level 0 (At most once)
Broker
Topic
Client 1
 You have a complete or almost stable connection between sender and receiver. A classic
use case is when connecting a test client or a front end application to a MQTT broker
over a wired connection.
 You dont care if one or more messages are lost once a while. That is sometimes the case
if the data is not that important or will be send at short intervals, where it is okay
that messages might get lost.
 You dont need any message queuing. Messages are only queued for disconnected clients if
they have QoS 1 or 2 and a persistent session.
Quality of Service Level 1 (At least once)
Broker
Topic
Client 1
 You need to get every message and your use case can handle duplicates. The most often
used QoS is level 1, because it guarantees the message arrives at least once. Of course
your application must be tolerating duplicates and process them accordingly.
 You cant bear the overhead of QoS 2. Of course QoS 1 is a lot fast in delivering
messages without the guarantee of level 2.
Quality of Service Level 2 (Exactly once)
Broker
Topic
Client 1
 It is critical to your application to receive all messages exactly once. This is often
the case if a duplicate delivery would do harm to application users or subscribing
clients. You should be aware of the overhead and that it takes a bit longer to complete
the QoS 2 flow.
Retain bit
Name Bit position Description
DUP 3 Duplicate delivery
QoS 2-1 Quality of Service
RETAIN 0 RETAIN flag
This flag is only used on PUBLISH messages. When a client sends a PUBLISH to a
server, if the Retain flag is set (1), the server should hold on to the message after
it has been delivered to the current subscribers.
When a new subscription is established on a topic, the last retained message on that
topic should be sent to the subscriber with the Retain flag set. If there is no
retained message, nothing is sent
Authorization and Authentication
const mosca = require('mosca');
const server = new mosca.Server({});
const authenticate = (client, username, password, callback) => {
const authorized = (username === 'gdg' && password.toString() === '123');
if (authorized) client.user = username;
callback(null, authorized);
}
const authorizePublish = (client, topic, payload, callback) => {
callback(null, true);
}
const authorizeSubscribe = (client, topic, callback) => {
callback(null, true);
}
server.on('ready', () => {
server.authenticate = authenticate;
server.authorizePublish = authorizePublish;
server.authorizeSubscribe = authorizeSubscribe;
});
Any question?
github.com/henricavalcante
twitter.com/henricavalcante

More Related Content

MQTT

  • 1. MQTT A nice way to connect 'things' in IoT
  • 3. Message Queue Telemetry Transport is a publish-subscribe lightweight protocol who runs over TCP protocol, it's very fast and simple and runs pretty well over high latencies and unreliable networks. What is it?
  • 4. NodeJS Installation $ curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash - $ sudo apt-get install -y nodejs $ brew install node $ curl --silent --location https://rpm.nodesource.com/setup_5.x | bash - $ yum -y install nodejs Debian / Ubuntu Red Hat / CentOS / Fedora OSX Windows https://nodejs.org/en/download/
  • 5. MQTT Installation $ sudo npm install --global mqtt ClientServer (Mosca.JS) $ sudo npm install --global mosca bunyan $ mosca -v +++.+++: ,+++ +++; '+++ +++. ++.+++.++ ++.++ ++,'+ `+',++ ++,++ +` +, +: .+ .+ +; +; '+ '+ +` +` +` +. +: ,+ `+ ++ +; '+ ;+ + +. +` +. +: ,+ `+ +' '+ + +. +` +. +: ,+ `+ :+. '+ +++++. +` +. +: ,+ `+ ++ '+ +++++. +` +. +: ,+ `+ ++ '+ + +. +` +. +: ,+ `+ +: +: '+ ;+ + +. +` +. +: .+ .+ +; +; '+ '+ + +. +` +. +: ++;++ ++'++ ++'+' + +. +` +. +: +++ +++. ,++' + +. $ mqtt sub -h localhost -t hello -v $ mqtt pub -h localhost -t hello -m GDGNatal
  • 6. Standard for IoT OASIS is pleased to announce that MQTT Version 3.1.1 from the OASIS Message Queuing Telemetry Transport (MQTT) TC has been approved by the membership as an OASIS Standard.
  • 8. bit 7 6 5 4 3 2 1 0 Byte 1 Message Type DUP flag QoS level RETAIN Byte 2 Remaining Length Lightweight MQTT Header $ echo "GET / HTTP/1.1" | wc -c 15 Bytes $ curl "http://localhost:1880/teste/" -v 2>&1 | grep '>' | cut -c 3- | wc - c 84 Bytes HTTP Header
  • 9. Message Type Mnemonic Enum Description Reserved 0 Reserved CONNECT 1 Client request to connect to Server CONNACK 2 Connect Acknowledgment PUBLISH 3 Publish message PUBACK 4 Publish Acknowledgment PUBREC 5 Publish Received (assured delivery part 1) PUBREL 6 Publish Release (assured delivery part 2) PUBCOMP 7 Publish Complete (assured delivery part 3) SUBSCRIBE 8 Client Subscribe request SUBACK 9 Subscribe Acknowledgment UNSUBSCRIBE 10 Client Unsubscribe request UNSUBACK 11 Unsubscribe Acknowledgment PINGREQ 12 PING Request PINGRESP 13 PING Response DISCONNECT 14 Client is Disconnecting Reserved 15 Reserved Position: byte 1, bits 7-4. Represented as a 4-bit unsigned value. The enumerations for this version of the protocol are shown in the table below.
  • 10. Flags Name Bit position Description DUP 3 Duplicate delivery QoS 2-1 Quality of Service RETAIN 0 RETAIN flag
  • 11. DUP Flag This flag is set when the client or server attempts to re-deliver a PUBLISH, PUBREL, SUBSCRIBE or UNSUBSCRIBE message. This applies to messages where the value of QoS is greater than zero (0), and an acknowledgment is required. When the DUP bit is set, the variable header includes a Message ID. The recipient should treat this flag as a hint as to whether the message may have been previously received. It should not be relied on to detect duplicates. Name Bit position Description DUP 3 Duplicate delivery QoS 2-1 Quality of Service RETAIN 0 RETAIN flag
  • 12. Quality of Service Level QOS Level Description 0 (00) At most once 1 (01) At least once 2 (10) Exactly once Name Bit position Description DUP 3 Duplicate delivery QoS 2-1 Quality of Service RETAIN 0 RETAIN flag
  • 13. Quality of Service Level 0 (At most once) Broker Topic Client 1 You have a complete or almost stable connection between sender and receiver. A classic use case is when connecting a test client or a front end application to a MQTT broker over a wired connection. You dont care if one or more messages are lost once a while. That is sometimes the case if the data is not that important or will be send at short intervals, where it is okay that messages might get lost. You dont need any message queuing. Messages are only queued for disconnected clients if they have QoS 1 or 2 and a persistent session.
  • 14. Quality of Service Level 1 (At least once) Broker Topic Client 1 You need to get every message and your use case can handle duplicates. The most often used QoS is level 1, because it guarantees the message arrives at least once. Of course your application must be tolerating duplicates and process them accordingly. You cant bear the overhead of QoS 2. Of course QoS 1 is a lot fast in delivering messages without the guarantee of level 2.
  • 15. Quality of Service Level 2 (Exactly once) Broker Topic Client 1 It is critical to your application to receive all messages exactly once. This is often the case if a duplicate delivery would do harm to application users or subscribing clients. You should be aware of the overhead and that it takes a bit longer to complete the QoS 2 flow.
  • 16. Retain bit Name Bit position Description DUP 3 Duplicate delivery QoS 2-1 Quality of Service RETAIN 0 RETAIN flag This flag is only used on PUBLISH messages. When a client sends a PUBLISH to a server, if the Retain flag is set (1), the server should hold on to the message after it has been delivered to the current subscribers. When a new subscription is established on a topic, the last retained message on that topic should be sent to the subscriber with the Retain flag set. If there is no retained message, nothing is sent
  • 17. Authorization and Authentication const mosca = require('mosca'); const server = new mosca.Server({}); const authenticate = (client, username, password, callback) => { const authorized = (username === 'gdg' && password.toString() === '123'); if (authorized) client.user = username; callback(null, authorized); } const authorizePublish = (client, topic, payload, callback) => { callback(null, true); } const authorizeSubscribe = (client, topic, callback) => { callback(null, true); } server.on('ready', () => { server.authenticate = authenticate; server.authorizePublish = authorizePublish; server.authorizeSubscribe = authorizeSubscribe; });