Post on 11-Sep-2014
description
Dominik Obermaier | dc-square GmbH
M2M for Java Developers with MQTT
About meHi, I’m Dominik Obermaier.• CTO of dc-square
• HiveMQ architect
• helped standardizing MQTT at OASIS
• Speaker and author about MQTT, M2M and IoT
@dobermai
• Macbook E-Mail Stockphoto
https://flic.kr/p/aERxbF
• Imagine Bild
• Internet of Things
Internet of Things
The Internet of Things (IoT) refers to uniquely identifiable objects and their virtual representations in an
Internet-like structure
Why should
we care?
Some statistics
2010 2015 2020
~6.909.000.000 ~7.302.000.000 ~7.675.000.000
~12.500.000.000 ~25.000.000.000 ~50.000.000.000
[1]Source: http://www.un.org/esa/population/publications/wpp2008/wpp2008_highlights.pdf
[1]
[2]Source: http://share.cisco.com/internet-of-things.html
[2]
Some statistics
0
12,500,000,000
25,000,000,000
37,500,000,000
50,000,000,000
2010 2015 2020
People Things
Some statistics
0
12,500,000,000
25,000,000,000
37,500,000,000
50,000,000,000
2010 2015 2020
People Things
1.8x
Some statistics
0
12,500,000,000
25,000,000,000
37,500,000,000
50,000,000,000
2010 2015 2020
People Things
1.8x
3.4x
Some statistics
0
12,500,000,000
25,000,000,000
37,500,000,000
50,000,000,000
2010 2015 2020
People Things
1.8x
3.4x
6.5x
Evolution?
Revolution?
TECHNOLOGY THAT SUPPORTS WIRED
OR WIRELESS COMMUNICATION BETWEEN DEVICES
M2MInternet of Things
RFID
Bluetooth
Barcode
Non-IP
Why now?
Screenshot Taken from Oracle JavaOne Strategic Keynote 2013
ChallengesSecurity Identity
ManagementInteroperability
PrivacyProvisioning
ScalabilityBIG Data
Device Management
...
Protocols?
Application Protocols
CoAP
HTTP
‣ efficient in bandwidth
‣ scalable
‣ standardized
‣ open
‣ data agnostic
‣ must be suited for constrained devices AND server infrastructure
Requirements for an IoT Protocol
Request / Response
Verbose
Polling instead of Push
No quality of service
How to get notified if clients die?
Stateless
HTTP?
What is MQTT‣ Messaging Protocol
‣ Simple
‣ On top of TCP
‣ Publish / Subscribe Architecture
‣ Binary protocol
‣ Minimal Overhead
‣ Designed for unreliable networks
‣ Data agnostic
Use cases‣ Push instead of Poll
‣ Bandwidth is at a premium
‣ Enterprise applications should interact with mobile applications
‣ Reliable delivery of messages over unreliable networks
‣ Constrained devices
‣ Low latency
Features‣ Topic Wildcards
‣ 3 Quality of Service Levels
‣ Retained Messages
‣ Last Will and Testament
‣ Persistent Sessions
‣ Heartbeats
Publish / Subscribe
Publish / Subscribe
MQTT Topics
‣ Hierarchical
‣ Very lightweight
‣ Subtopic-Level Wildcards (+)
‣ Subtree-Level Wildcards (#)
‣ Max 64.000 characters
‣ Clients can publish / subscribe to N topics
‣ Brokers can queue for topics if QoS > 0
MQTT Topics
MQTT Topics
MQTT Topicshouse1/floor1/kitchen/light
MQTT Topicshouse1/floor1/kitchen/light
house1/floor1/kitchen/+
MQTT Topicshouse1/floor1/kitchen/light
house1/floor1/kitchen/+
house1/floor1/+/light
MQTT Topicshouse1/floor1/kitchen/light
house1/floor1/kitchen/+
house1/floor1/+/light
house1/+/+/light
MQTT Topicshouse1/floor1/kitchen/light
house1/floor1/kitchen/+
house1/floor1/+/light
house1/+/+/light
house1/#
MQTT Topicshouse1/floor1/kitchen/light
house1/floor1/kitchen/+
house1/floor1/+/light
house1/+/+/light
house1/#
+/floor1/+/#
MQTT Topicshouse1/floor1/kitchen/light
house1/floor1/kitchen/+
house1/floor1/+/light
house1/+/+/light
house1/#
+/floor1/+/#
+/+/+/#
MQTT Topics
MQTT/is/awesome MQTT/is/fantastic
Bacon/is/awesome
MQTT/looks/fantasticBacon/looks/fantastic
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
MQTT/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
MQTT/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
MQTT/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
MQTT/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
MQTT/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
MQTT/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
Bacon/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
Bacon/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
Bacon/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
Bacon/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
Bacon/is/awesome
MQTT Topics
MQTT Bacon
is
awesome
looks
fantastic
Bacon/is/awesome
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/awesome
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/awesome
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/awesome
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/awesome
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/awesome
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/awesome
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/awesome
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/+
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/+
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/+
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/+
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/+
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/+
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/+
MQTT Topic Level Wildcard
MQTT Bacon
is
awesome
looks
fantastic
+/is/+
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
MQTT/#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
MQTT/#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
MQTT/#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
MQTT/#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
MQTT/#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
MQTT/#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
MQTT/#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
MQTT/#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
MQTT/#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
MQTT Subtree Wildcard
MQTT Bacon
is
awesome
looks
fantastic
#
Quality of Service Levels
QoS 0 At most once delivery
The message is delivered once or never.
QoS 1 At least once delivery
The message is delivered once or more.
QoS 2 Exactly once delivery
The message is delivered exactly once.
Last Will and Testament‣ Clients can specify a LWT
‣ Broker publishes the LWT message on behalf of the client on “death”
‣ Useful for reporting problems
‣ Real push on device “death”
‣ Mostly used for reporting the connection status of a device
Retained messages‣ Last known good value
‣ Last message is stored for a topic
‣ Publishing clients decide if the message should be retained
‣ Clients automatically receive the retained message after subscribing
SecurityProtocol
‣ Username / Password
‣ Payload Encryption
Transport
‣ TLS
‣ Client certificate authentication
Broker
‣ Publish / Subscribe Permissions
‣ Integration to other systems (databases, APIs, ....)
History
1999 2010 2013 2014
MQTT inventedby IBM and
Arcom (Eurotech)
royalty free OASIS TC formed
MQTT is OASIS standard
Real world use cases
Use case:
‣ more than 850M Facebook Messenger Users
‣ Chat application
‣ Improved Battery
‣ Lower latency
‣ Less bandwidth
https://www.facebook.com/notes/facebook-engineering/building-facebook-messenger/10150259350998920
Facebook Messenger
Use case:
‣MQTT as central message “bus”
‣ Easy to integrate with other technologies
‣ Remote monitoring
‣ Many open source Smart Home Frameworks
support MQTT
‣ Remote control
Smart Home
Use case:
‣30.000 devices
‣ 17.000 km pipeline
‣ Remote monitoring
‣ Remote control
‣ Uses satellite links
‣ Bandwidth is very expensive
Pipelines
Gaining traction
Google Trends - 2014/05/13
MQTT Brokers
Mosquitto
‣ Open Source
‣ Ideal for running in constrained environments
‣ Supports Bridging
‣Written in C
HiveMQ‣ High Performance
MQTT broker
‣ Native Websockets Support
‣ Java Plugin System
‣ Clustering
‣ Bridging
‣ Scalable to > 100.000 connections
Other brokers
+ Many otherssee https://github.com/mqtt/mqtt.github.io/wiki/brokers
Scaling
HIER SCALING BILD
Bridging
Clustering
Broker comparisonQoS
0QoS
1QoS
2Auth Bridge $SYS SSL dynamic
topicscluster websockets plugins
HiveMQ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
mosquitto ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ X X ✔
RSMB ✔ ✔ ✔ ✔ ✔ ✔ X ✔ X X X
RabbitMQ ✔ ✔ X ✔ X X ✔ ✔ ? ? ?
ActiveMQ ✔ ✔ ✔ ? X X ? ? ? ✔ ?
MQTT Clients
Eclipse Paho‣ Open Source
‣ “Reference Implementation”
‣ Available in many languages: Java, Javascript, Lua, C, C++, Go, Python
‣ The JS library uses websockets
‣ Active Community
MqttClient client = new MqttClient( "tcp://localhost:1883", //URI "publisher", //Client ID new MemoryPersistence()); //Persistence
client.connect();
client.publish("the/topic", //topic "message".getBytes(), //message 1, //QoS false); //retained
client.disconnect();
MqttClient client = new MqttClient( "tcp://localhost:1883", //URI "publisher", //Client ID new MemoryPersistence()); //Persistence
MqttConnectOptions connOptions = new MqttConnectOptions();
connOptions.setKeepAliveInterval(120); connOptions.setWill("help/I/died",
new byte[0], 2, true); connOptions.setCleanSession(false); connOptions.setUserName("username"); connOptions.setPassword("passw".toCharArray());
client.connect(connOptions);
MqttAsyncClient client = new MqttAsyncClient(...);
client.connect(null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { try { client.publish(...); } catch (MqttException e) {} }
@Override public void onFailure(IMqttToken asyncActionToken,
Throwable exception) {} }); client.disconnect();
final MqttClient client = new MqttClient(...);
client.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable cause) {}
@Override public void messageArrived(String topic,
MqttMessage message)throws Exception { System.out.println(new String(message.getPayload())); }
@Override public void deliveryComplete(IMqttDeliveryToken token) {} }); client.connect();
client.subscribe("#");
Fusesource MQTT Client
‣ Java
‣ 3 API Styles
‣ Very easy to intercept
‣ Based on HawtIO
‣ Very performant
‣ In Maven Central
MQTT mqtt = new MQTT(); mqtt.setHost("localhost", 1883); mqtt.setClientId("clientId"); mqtt.setCleanSession(false);
mqtt.setWillTopic("willtopic"); mqtt.setWillMessage("willmessage"); mqtt.setWillQos(QoS.EXACTLY_ONCE);
mqtt.setUserName("user"); mqtt.setPassword("passwd");
BlockingConnection conn = mqtt.blockingConnection();
conn.connect();
MQTT mqtt = new MQTT(); BlockingConnection conn =
mqtt.blockingConnection(); conn.connect();
conn.publish("the/topic", //topic "message".getBytes(), //message QoS.EXACTLY_ONCE, //QoS false);
conn.disconnect();
MQTT mqtt = new MQTT();
FutureConnection conn = mqtt.futureConnection(); Future<Void> connectFuture = conn.connect();
// funky business logic
connectFuture.await();
conn.publish("the/topic", //topic "message".getBytes(), //message QoS.EXACTLY_ONCE, //QoS false). then(new Callback<Void>() { @Override public void onSuccess(Void value) { System.out.println("Success"); }
@Override public void onFailure(Throwable value) {}});
MQTT mqtt = new MQTT(); CallbackConnection conn = mqtt.callbackConnection(); conn.connect(new Callback<Void>() { @Override public void onSuccess(Void value) { conn.publish("the/topic", //topic "message".getBytes(), //message QoS.EXACTLY_ONCE, //QoS false, //retain new Callback<Void>() { @Override public void onSuccess(Void value) { System.out.println("Published"); }
@Override public void onFailure(Throwable value) {} }); } @Override public void onFailure(Throwable value) {}});
Other MQTT Clients
https://github.com/mqtt/mqtt.github.io/wiki/libraries
MQTT over Websockets
‣ Every Browser is a ‘device’
‣ Device-to-Browser Push
‣ Broadcasting to all online users
‣ Detect if a user is offline
‣ Direct Browser-to-device push
‣ Minimum payload
‣ No application-specific protocol
MQTT over websockets
Thanks!@dobermai