Mobile devices like smartphones and tablet computers became an integral part of our modern world and single-board computers like Raspberry Pi are cheaper today than at any time before. Simple and open Machine-to-Machine (M2M) protocols like MQTT enable these devices to communicate in an efficient manner, even in scenarios with unreliable and instable networks. This talk shows how different Java MQTT libraries can be utilized for professional and personal projects to build efficient and scalable solutions for (mobile) devices. You will also see how modern technologies like WebSocket can be used in conjunction with MQTT to build real time-push services to bring data from sensor hardware to browsers in a scalable manner.
2. About me
Hi, I’m Dominik Obermaier.
• CTO of dc-square
• HiveMQ architect
• helped standardizing MQTT at OASIS
• Speaker and author about MQTT, M2M
and IoT
@dobermai
35. ‣ efficient in bandwidth
‣ scalable
‣ standardized
‣ open
‣ data agnostic
‣ must be suited for constrained
devices AND server infrastructure
Requirements for an IoT Protocol
38. What is MQTT
‣ Messaging Protocol
‣ Simple
‣ On top of TCP
‣ Publish / Subscribe
Architecture
‣ Binary protocol
‣ Minimal Overhead
‣ Designed for unreliable
networks
‣ Data agnostic
39. 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
40. Features
‣ Topic Wildcards
‣ 3 Quality of Service
Levels
‣ Retained Messages
‣ Last Will and
Testament
‣ Persistent Sessions
‣ Heartbeats
44. ‣ 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
100. 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.
101. 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
102. 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
103. Security
Protocol
‣ Username / Password
‣ Payload Encryption
Transport
‣ TLS
‣ Client certificate
authentication
Broker
‣ Publish / Subscribe
Permissions
‣ Integration to other systems
(databases, APIs, ....)
104. History
1999 2010 2013 2014
MQTT invented
by IBM and
Arcom
(Eurotech)
royalty free OASIS TC
formed
MQTT is
OASIS
standard
106. 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
107. 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
108. Use case:
‣30.000 devices
‣ 17.000 km pipeline
‣ Remote monitoring
‣ Remote control
‣ Uses satellite links
‣ Bandwidth is very expensive
Pipelines
119. Eclipse Paho
‣ Open Source
‣ “Reference
Implementation”
‣ Available in many
languages: Java,
Javascript, Lua, C, C++,
Go, Python
‣ The JS library uses
websockets
‣ Active Community
120. 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();
121. 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);
122. 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();
123. 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("#");
124. Fusesource MQTT Client
‣ Java
‣ 3 API Styles
‣ Very easy to
intercept
‣ Based on HawtIO
‣ Very performant
‣ In Maven Central
131. ‣ 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