JAX 2014 - M2M for Java Developers with MQTT

  • 2,954 views
Uploaded on

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 …

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.

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,954
On Slideshare
0
From Embeds
0
Number of Embeds
12

Actions

Shares
Downloads
113
Comments
0
Likes
10

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Dominik Obermaier | dc-square GmbH M2M for Java Developers with MQTT
  • 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
  • 3. • Typical morning https://flic.kr/p/eaX1GC
  • 4. https://flic.kr/p/7Vvzvp
  • 5. https://flic.kr/p/48VxKfhttps://flic.kr/p/hLd8CU
  • 6. • Macbook E-Mail Stockphoto https://flic.kr/p/aERxbF
  • 7. • Dusche Stockphoto https://flic.kr/p/6JuGDb
  • 8. https://flic.kr/p/3QYv2o
  • 9. • Imagine Bild
  • 10. • Internet of Things Internet of Things
  • 11. The Internet of Things (IoT) refers to uniquely identifiable objects and their virtual representations in an Internet-like structure
  • 12. Why should we care?
  • 13. 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]
  • 14. Some statistics 0 12,500,000,000 25,000,000,000 37,500,000,000 50,000,000,000 2010 2015 2020 People Things
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. https://flic.kr/p/athSFb
  • 19. • Android Photo https://flic.kr/p/9vSR9i
  • 20. Evolution? Revolution?
  • 21. M2M? https://flic.kr/p/834e93
  • 22. TECHNOLOGY THAT SUPPORTS WIRED OR WIRELESS COMMUNICATION BETWEEN DEVICES
  • 23. M2MInternet of Things RFID Bluetooth Barcode Non-IP
  • 24. Why now?
  • 25. https://flic.kr/p/8EidjG
  • 26. https://flic.kr/p/daTVZY
  • 27. Screenshot Taken from Oracle JavaOne Strategic Keynote 2013
  • 28. Are we there yet? https://flic.kr/p/5WR2fd
  • 29. Challenges Security Identity Management Interoperability Privacy Provisioning Scalability BIG Data Device Management ...
  • 30. Protocols?
  • 31. Application Protocols CoAP HTTP
  • 32. ‣ efficient in bandwidth ‣ scalable ‣ standardized ‣ open ‣ data agnostic ‣ must be suited for constrained devices AND server infrastructure Requirements for an IoT Protocol
  • 33. Request / Response Verbose Polling instead of Push No quality of service How to get notified if clients die? Stateless HTTP?
  • 34. What is MQTT ‣ Messaging Protocol ‣ Simple ‣ On top of TCP ‣ Publish / Subscribe Architecture ‣ Binary protocol ‣ Minimal Overhead ‣ Designed for unreliable networks ‣ Data agnostic
  • 35. 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
  • 36. Features ‣ Topic Wildcards ‣ 3 Quality of Service Levels ‣ Retained Messages ‣ Last Will and Testament ‣ Persistent Sessions ‣ Heartbeats
  • 37. Publish / Subscribe
  • 38. Publish / Subscribe
  • 39. MQTT Topics
  • 40. ‣ 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
  • 41. MQTT Topics
  • 42. MQTT Topics house1/floor1/kitchen/light
  • 43. MQTT Topics house1/floor1/kitchen/light house1/floor1/kitchen/+
  • 44. MQTT Topics house1/floor1/kitchen/light house1/floor1/kitchen/+ house1/floor1/+/light
  • 45. MQTT Topics house1/floor1/kitchen/light house1/floor1/kitchen/+ house1/floor1/+/light house1/+/+/light
  • 46. MQTT Topics house1/floor1/kitchen/light house1/floor1/kitchen/+ house1/floor1/+/light house1/+/+/light house1/#
  • 47. MQTT Topics house1/floor1/kitchen/light house1/floor1/kitchen/+ house1/floor1/+/light house1/+/+/light house1/# +/floor1/+/#
  • 48. MQTT Topics house1/floor1/kitchen/light house1/floor1/kitchen/+ house1/floor1/+/light house1/+/+/light house1/# +/floor1/+/# +/+/+/#
  • 49. MQTT Topics MQTT/is/awesome MQTT/is/fantastic Bacon/is/awesome MQTT/looks/fantasticBacon/looks/fantastic
  • 50. MQTT Topics MQTT Bacon is awesome looks fantastic MQTT/is/awesome
  • 51. MQTT Topics MQTT Bacon is awesome looks fantastic MQTT/is/awesome
  • 52. MQTT Topics MQTT Bacon is awesome looks fantastic MQTT/is/awesome
  • 53. MQTT Topics MQTT Bacon is awesome looks fantastic MQTT/is/awesome
  • 54. MQTT Topics MQTT Bacon is awesome looks fantastic MQTT/is/awesome
  • 55. MQTT Topics MQTT Bacon is awesome looks fantastic MQTT/is/awesome
  • 56. MQTT Topics MQTT Bacon is awesome looks fantastic Bacon/is/awesome
  • 57. MQTT Topics MQTT Bacon is awesome looks fantastic Bacon/is/awesome
  • 58. MQTT Topics MQTT Bacon is awesome looks fantastic Bacon/is/awesome
  • 59. MQTT Topics MQTT Bacon is awesome looks fantastic Bacon/is/awesome
  • 60. MQTT Topics MQTT Bacon is awesome looks fantastic Bacon/is/awesome
  • 61. MQTT Topics MQTT Bacon is awesome looks fantastic Bacon/is/awesome
  • 62. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/awesome
  • 63. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/awesome
  • 64. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/awesome
  • 65. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/awesome
  • 66. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/awesome
  • 67. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/awesome
  • 68. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/awesome
  • 69. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/+
  • 70. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/+
  • 71. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/+
  • 72. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/+
  • 73. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/+
  • 74. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/+
  • 75. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/+
  • 76. MQTT Topic Level Wildcard MQTT Bacon is awesome looks fantastic +/is/+
  • 77. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic MQTT/#
  • 78. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic MQTT/#
  • 79. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic MQTT/#
  • 80. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic MQTT/#
  • 81. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic MQTT/#
  • 82. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic MQTT/#
  • 83. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic MQTT/#
  • 84. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic MQTT/#
  • 85. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic MQTT/#
  • 86. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 87. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 88. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 89. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 90. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 91. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 92. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 93. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 94. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 95. MQTT Subtree Wildcard MQTT Bacon is awesome looks fantastic #
  • 96. 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.
  • 97. 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
  • 98. 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
  • 99. Security Protocol ‣ Username / Password ‣ Payload Encryption Transport ‣ TLS ‣ Client certificate authentication Broker ‣ Publish / Subscribe Permissions ‣ Integration to other systems (databases, APIs, ....)
  • 100. History 1999 2010 2013 2014 MQTT invented by IBM and Arcom (Eurotech) royalty free OASIS TC formed MQTT is OASIS standard
  • 101. Real world use cases
  • 102. 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
  • 103. 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
  • 104. Use case: ‣30.000 devices ‣ 17.000 km pipeline ‣ Remote monitoring ‣ Remote control ‣ Uses satellite links ‣ Bandwidth is very expensive Pipelines
  • 105. Gaining traction Google Trends - 2014/05/13
  • 106. MQTT Brokers
  • 107. Mosquitto ‣ Open Source ‣ Ideal for running in constrained environments ‣ Supports Bridging ‣ Written in C
  • 108. HiveMQ ‣ High Performance MQTT broker ‣ Native Websockets Support ‣ Java Plugin System ‣ Clustering ‣ Bridging ‣ Scalable to > 100.000 connections
  • 109. Other brokers + Many others see https://github.com/mqtt/mqtt.github.io/wiki/brokers
  • 110. Scaling HIER SCALING BILD
  • 111. Bridging
  • 112. Clustering
  • 113. Broker comparison QoS 0 QoS 1 QoS 2 Auth Bridge $SYS SSL dynamic topics cluster websockets plugins HiveMQ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ mosquitto ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ X X ✔ RSMB ✔ ✔ ✔ ✔ ✔ ✔ X ✔ X X X RabbitMQ ✔ ✔ X ✔ X X ✔ ✔ ? ? ? ActiveMQ ✔ ✔ ✔ ? X X ? ? ? ✔ ?
  • 114. MQTT Clients
  • 115. Eclipse Paho ‣ Open Source ‣ “Reference Implementation” ‣ Available in many languages: Java, Javascript, Lua, C, C++, Go, Python ‣ The JS library uses websockets ‣ Active Community
  • 116. 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();
  • 117. 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);
  • 118. 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();
  • 119. 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("#");
  • 120. Fusesource MQTT Client ‣ Java ‣ 3 API Styles ‣ Very easy to intercept ‣ Based on HawtIO ‣ Very performant ‣ In Maven Central
  • 121. 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();
  • 122. 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();
  • 123. 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) {}});
  • 124. 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) {}});
  • 125. Other MQTT Clients https://github.com/mqtt/mqtt.github.io/wiki/libraries
  • 126. MQTT over Websockets
  • 127. ‣ 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
  • 128. Thanks! @dobermai