IBM® WebSphere® Front cover Building Real-time Mobile Solutions with MQTT and IBM MessageSight
Provides practical guidance to getting started quickly with MQTT and IBM MessageSight
Builds a mobile application (PickMeUp) by using MQTT and IBM MessageSight
Shows typical usage patterns and guidance to expand the solution
Bryan Boyd Joel Gauci Michael P Robertson Nguyen Van Duy Rahul Gupta Vasfi Gucer Vladimir Kislicins
ibm.com/redbooks
International Technical Support Organization
Building Real-time Mobile Solutions with MQTT and IBM MessageSight
October 2014
SG24-8228-00 Note: Before using this information and the product it supports, read the information in “Notices” on page vii.
First Edition (October 2014)
This edition applies to IBM MessageSight Version 1.1 and MQTT Version 3.1.
© Copyright International Business Machines Corporation 2014. All rights reserved. Note to U.S. Government Users Restricted Rights -- Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents
Notices ...... vii Trademarks ...... viii
Preface ...... ix Authors...... ix Now you can become a published author, too! ...... xi Comments welcome...... xi Stay connected to IBM Redbooks ...... xii
Chapter 1. Overview of MQTT ...... 1 1.1 Building a Smarter Planet world ...... 2 1.1.1 The Internet of Things (IoT) ...... 2 1.1.2 Smarter Planet concept ...... 4 1.1.3 Telemetry and the Internet ...... 5 1.2 MQTT ...... 6 1.2.1 Benefits of the MQTT protocol ...... 6 1.2.2 Basic concepts of the MQTT protocol...... 7 1.2.3 The OASIS MQTT Technical Committee ...... 8 1.2.4 The Eclipse Paho project ...... 9 1.2.5 Comparison of MQTT and HTTP ...... 10 1.3 MessageSight ...... 12 1.4 Benefits of using MQTT ...... 15 1.5 Where to use MQTT ...... 17 1.5.1 Connected car...... 17 1.5.2 Connected city ...... 17 1.5.3 Connected home...... 18 1.5.4 Connected consumers ...... 19
Chapter 2. Getting started with MQTT...... 21 2.1 MQTT concepts...... 22 2.1.1 MQTT messaging ...... 22 2.1.2 MQTT client programming ...... 24 2.2 MQTT server ...... 25 2.2.1 IBM MessageSight ...... 25 2.2.2 IBM WebSphere MQ...... 25 2.2.3 Really Small Message Broker and MQ Telemetry daemon for devices ...... 25 2.2.4 Mosquitto ...... 25 2.3 MQTT clients...... 26 2.3.1 Eclipse Paho clients ...... 26 2.3.2 IBM Mobile Messaging and M2M Client Pack MA9B ...... 26 2.3.3 Preparation ...... 27 2.3.4 Building the sample MQTT application ...... 29 2.3.5 MQTT publisher and subscriber in Java ...... 30 2.3.6 MQTT publisher and subscriber in JavaScript ...... 38
Chapter 3. Overview of IBM MessageSight...... 47 3.1 Features of MessageSight ...... 48 3.1.1 MessageSight is a developer-friendly solution ...... 49 3.1.2 Connections to MessageSight ...... 50
© Copyright IBM Corp. 2014. All rights reserved. iii 3.2 Messaging patterns of MessageSight ...... 50 3.2.1 Fan out broadcast ...... 50 3.2.2 Fan in per device notification ...... 51 3.2.3 Fan out per device notification ...... 52 3.2.4 Fan out per device request-reply ...... 53 3.2.5 Fan in per device request-reply...... 53 3.3 Install the MessageSight virtual appliance (for developers) ...... 54 3.4 Overview of the MessageSight web UI ...... 57 3.4.1 Connect to the MessageSight appliance ...... 57 3.4.2 The MessageSight Home page ...... 60 3.4.3 Administrator actions using the MessageSight web UI...... 63 3.5 Overview of the MessageSight CLI...... 70 3.5.1 Connect to the MessageSight appliance ...... 70 3.5.2 Administrator actions using the MessageSight CLI...... 71 3.6 Message hubs, endpoints, and policies ...... 75 3.6.1 Endpoints ...... 75 3.6.2 Message hubs...... 76 3.6.3 Connection policies...... 77 3.6.4 Messaging policies ...... 77 3.6.5 Endpoints ...... 78 3.6.6 The DemoHub message hub ...... 78 3.6.7 Configuring your first message hub using the MessageSight web UI...... 82 3.6.8 Configuring a message hub using the MessageSight CLI ...... 82 3.6.9 Use the MessageSight SSH to deploy message hub configuration ...... 84
Chapter 4. Typical network topology, messaging patterns, and considerations . . . . 87 4.1 Network topology ...... 88 4.1.1 The architecture ...... 91 4.2 Messaging patterns...... 91 4.2.1 Fan out broadcast ...... 92 4.2.2 Fan in per device notification ...... 93 4.2.3 Fan out per device notification ...... 94 4.2.4 Fan in per device request reply...... 97 4.2.5 Fan out per device request reply ...... 99 4.3 Messaging considerations...... 102 4.3.1 Quality of service ...... 102 4.3.2 Message size ...... 103 4.3.3 Message order ...... 103 4.3.4 Topic namespace ...... 103 4.3.5 Retained message ...... 103
Chapter 5. IBM MessageSight and the key fob remote application ...... 105 5.1 Overview of the key fob remote application ...... 106 5.1.1 Application overview ...... 106 5.1.2 Testing the key fob remote application ...... 107 5.2 MessageSight configurations for the key fob remote application ...... 109 5.2.1 MessageSight basic configuration ...... 109 5.3 Security capabilities of the MessageSight appliance ...... 119 5.3.1 Adding security controls to the key fob remote application...... 120 5.3.2 Adding security at the transport level using SSL or TLS...... 135
Chapter 6. Overview of the PickMeUp application...... 147 6.1 Company A scenario...... 148 6.1.1 Company A business problem ...... 148 iv Building Real-time Mobile Solutions with MQTT and IBM MessageSight 6.1.2 Requirements for the PickMeUp application at Company A ...... 148 6.2 PickMeUp architecture for real-life use ...... 153 6.2.1 Architecture overview ...... 153 6.2.2 Considerations for the real-life use of PickMeUp ...... 155 6.3 Company A solution ...... 163 6.3.1 Scenario 1: Connecting...... 164 6.3.2 Scenario 2: Pairing ...... 165 6.3.3 Scenario 3: Approaching...... 165 6.3.4 Scenario 4: Riding...... 167 6.3.5 Scenario 5: Payment...... 168
Chapter 7. PickMeUp messaging scenario ...... 171 7.1 Actors in the PickMeUp solution ...... 172 7.2 Stages of the PickMeUp solution ...... 173 7.3 Topics and messages for the PickMeUp scenario ...... 174 7.3.1 Connecting ...... 174 7.3.2 Pairing...... 176 7.3.3 Approaching ...... 179 7.3.4 Riding ...... 181 7.3.5 Payment ...... 183 7.4 Summary of publication and subscription topics for the PickMeUp solution ...... 185 7.4.1 Driver app topics ...... 186 7.4.2 Passenger app topics ...... 186 7.4.3 Back-end application topics ...... 187
Chapter 8. PickMeUp MQTT on iOS...... 189 8.1 Advantages of developing a native (iOS) passenger app ...... 190 8.1.1 Using the iOS MQTT client ...... 190 8.2 Features of the PickMeUp iOS passenger app...... 192 8.2.1 PickMeUp class overview ...... 192 8.2.2 Chat topic structure...... 193 8.2.3 Chat Implementation...... 193 8.2.4 The PickMeUp iOS passenger app storyboard...... 198
Chapter 9. PickMeUp MQTT on Android ...... 203 9.1 Advantages of developing an Android PickMeUp application...... 204 9.2 Features of the Android PickMeUp application ...... 204 9.2.1 PickMeUp class overview ...... 205 9.2.2 Using the Eclipse Paho Android service ...... 208 9.2.3 More information about the Android MQTT Client ...... 209 9.2.4 Payment message flow...... 209
Chapter 10. PickMeUp MQTT in HTML5 applications ...... 217 10.1 Advantages of developing an HTML5 PickMeUp application ...... 218 10.2 Features of the HTML5 MQTT application ...... 218 10.2.1 PickMeUp Messenger overview ...... 218 10.2.2 PickMeUp Messenger: The Driver and Passenger location flow ...... 222
Chapter 11. Download, deploy, and run PickMeUp in iOS, Android, and HTML environments ...... 223 11.1 Set up a PickMeUp iOS project...... 224 11.1.1 Prerequisites ...... 224 11.1.2 Obtain the PickMeUp iOS source code...... 224 11.1.3 Open the PickMeUp iOS project ...... 225
Contents v 11.1.4 Configure the project build settings...... 227 11.1.5 Run the application ...... 229 11.1.6 Run PickMeUp for your iOS project ...... 230 11.2 Set up a PickMeUp Android project ...... 230 11.2.1 Prerequisites ...... 230 11.2.2 Register with Google Maps API ...... 231 11.2.3 Android SDK Packages ...... 231 11.2.4 Run PickMeUp for your Android project ...... 231 11.3 Set up the PickMeUp back end...... 231 11.3.1 Prerequisites ...... 231 11.3.2 Register with Bluemix ...... 232 11.3.3 Download, deploy, and run PickMeUp ...... 232 11.4 Set up the PickMeUp HTML5 project ...... 233
Appendix A. The MQTT protocol ...... 235 Quality of service (QoS) levels and flow ...... 236 QoS Level 0: At most once delivery ...... 236 QoS Level 1: At least once delivery ...... 236 QoS Level 2: Exactly once delivery...... 237 Assumptions of QoS levels 1 and 2 ...... 238 QoS determination ...... 238 Impact of QoS level on performance ...... 239 The MQTT client identifier ...... 239 MQTT durable and non-durable subscribers ...... 240 MQTT persistence ...... 240 Message delivery from JMS to MQTT...... 241 The MQTT header ...... 241 The MQTT keep alive timer ...... 242 Delivery of the MQTT retry message ...... 243 The MQTT last will and testament ...... 243 The MQTT retained flag on messages...... 244 The TCP/IP stack ...... 244
Appendix B. Additional material ...... 245 Locating the web material ...... 245 Using the web material...... 245 System requirements for downloading the web material ...... 246 Downloading and extracting the web material ...... 246
Related publications ...... 247 IBM Redbooks ...... 247 Online resources ...... 247 Help from IBM ...... 249
vi Building Real-time Mobile Solutions with MQTT and IBM MessageSight Notices
This information was developed for products and services offered in the U.S.A.
IBM may not offer the products, services, or features discussed in this document in other countries. Consult your local IBM representative for information on the products and services currently available in your area. Any reference to an IBM product, program, or service is not intended to state or imply that only that IBM product, program, or service may be used. Any functionally equivalent product, program, or service that does not infringe any IBM intellectual property right may be used instead. However, it is the user's responsibility to evaluate and verify the operation of any non-IBM product, program, or service.
IBM may have patents or pending patent applications covering subject matter described in this document. The furnishing of this document does not grant you any license to these patents. You can send license inquiries, in writing, to: IBM Director of Licensing, IBM Corporation, North Castle Drive, Armonk, NY 10504-1785 U.S.A.
The following paragraph does not apply to the United Kingdom or any other country where such provisions are inconsistent with local law: INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THIS PUBLICATION "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Some states do not allow disclaimer of express or implied warranties in certain transactions, therefore, this statement may not apply to you.
This information could include technical inaccuracies or typographical errors. Changes are periodically made to the information herein; these changes will be incorporated in new editions of the publication. IBM may make improvements and/or changes in the product(s) and/or the program(s) described in this publication at any time without notice.
Any references in this information to non-IBM websites are provided for convenience only and do not in any manner serve as an endorsement of those websites. The materials at those websites are not part of the materials for this IBM product and use of those websites is at your own risk.
IBM may use or distribute any of the information you supply in any way it believes appropriate without incurring any obligation to you.
Any performance data contained herein was determined in a controlled environment. Therefore, the results obtained in other operating environments may vary significantly. Some measurements may have been made on development-level systems and there is no guarantee that these measurements will be the same on generally available systems. Furthermore, some measurements may have been estimated through extrapolation. Actual results may vary. Users of this document should verify the applicable data for their specific environment.
Information concerning non-IBM products was obtained from the suppliers of those products, their published announcements or other publicly available sources. IBM has not tested those products and cannot confirm the accuracy of performance, compatibility or any other claims related to non-IBM products. Questions on the capabilities of non-IBM products should be addressed to the suppliers of those products.
This information contains examples of data and reports used in daily business operations. To illustrate them as completely as possible, the examples include the names of individuals, companies, brands, and products. All of these names are fictitious and any similarity to the names and addresses used by an actual business enterprise is entirely coincidental.
COPYRIGHT LICENSE:
This information contains sample application programs in source language, which illustrate programming techniques on various operating platforms. You may copy, modify, and distribute these sample programs in any form without payment to IBM, for the purposes of developing, using, marketing or distributing application programs conforming to the application programming interface for the operating platform for which the sample programs are written. These examples have not been thoroughly tested under all conditions. IBM, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.
© Copyright IBM Corp. 2014. All rights reserved. vii Trademarks
IBM, the IBM logo, and ibm.com are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. These and other IBM trademarked terms are marked on their first occurrence in this information with the appropriate symbol (® or ™), indicating US registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at http://www.ibm.com/legal/copytrade.shtml
The following terms are trademarks of the International Business Machines Corporation in the United States, other countries, or both:
Bluemix™ IBM® Smarter Planet® DataPower® InfoSphere® WebSphere® developerWorks® Redbooks® Worklight® Global Technology Services® Redbooks (logo) ®
The following terms are trademarks of other companies:
Worklight is trademark or registered trademark of Worklight, an IBM Company.
ITIL is a registered trademark, and a registered community trademark of The Minister for the Cabinet Office, and is registered in the U.S. Patent and Trademark Office.
IT Infrastructure Library is a registered trademark of the Central Computer and Telecommunications Agency which is now part of the Office of Government Commerce.
Microsoft, Windows, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both.
Java, and all Java-based trademarks and logos are trademarks or registered trademarks of Oracle and/or its affiliates.
Other company, product, or service names may be trademarks or service marks of others.
viii Building Real-time Mobile Solutions with MQTT and IBM MessageSight Preface
MQTT is a messaging protocol designed for the Internet of Things (IoT). It is lightweight enough to be supported by the smallest devices, yet robust enough to ensure that important messages get to their destinations every time. With MQTT devices, such as energy meters, cars, trains, mobile phones and tablets, and personal health care devices, devices can communicate with each other and with other systems or applications.
IBM® MessageSight is a messaging appliance designed to handle the scale and security of a robust IoT solution. MessageSight allows you to easily secure connections, configure policies for messaging, and scale to up to a million concurrently connected devices.
This IBM Redbooks® publication introduces MQTT and MessageSight through a simple key fob remote MQTT application. It then dives into the architecture and development of a robust, cross-platform Ride Share and Taxi solution (PickMeUp) with real-time voice, GPS location sharing, and chat among a variety of mobile platforms. The publication also includes an addendum describing use cases in a variety of other domains, with sample messaging topology and suggestions for design.
Authors
This book was produced by a team of specialists from around the world working at the International Technical Support Organization, Austin Center.
Bryan Boyd is a Solutions Software Engineer for IBM MessageSight. His primary focus is to rapidly prototype solutions with MQTT, MessageSight, and analytics technologies in emerging industries. He owns and maintains http://m2m.demos.ibm.com, a demo collection that showcases dynamic HTML5 applications using IBM MessageSight and MQTT for real-time analytics, communication, and collaboration. Bryan has delivered technical presentations about MQTT and application development at industry conferences and developer events. Bryan holds a Master’s Degree in Computer Science from Texas A&M University.
Joel Gauci is a Certified IT Specialist in the IBM WebSphere® Software group in France. Since 2006, Joel has been working for leading European firms on projects including IBM DataPower®, MessageSight, and API Management in various sectors. As a Client Technical Professional, Joel mainly works on MessageSight and API Management selling opportunities. He assists potential customers from basic presentation to complex architecture definition in the Internet of Things domain. Joel has authored several IBM Redbooks publications and articles related to DataPower appliances. Joel holds a Master’s Degree in Computer Science and a Master’s Degree in Mechanics from the University Paris 6 in France.
© Copyright IBM Corp. 2014. All rights reserved. ix Michael P Robertson is a Software Developer for IBM MessageSight. His main focus has been on testing the client side of MessageSight with the Java Message Service (JMS) and MQTT protocols. In addition to MessageSight, Michael contributes to the development of the GoLang and Objective-C MQTT Client libraries developed by the Eclipse Paho project. He also works on developing demo applications for MessageSight and MQTT.
Nguyen Van Duy is an Advisory IT Architect with IBM Global Technology Services® in Vietnam. He is an IBM Associate Certified IT Architect with solid experience in IBM and open technologies. On his current assignment, Duy works as the Technical Leader for the IBM Global Procurement Services Group in Vietnam to provide enterprise software development services. He is focusing on mobile solutions, including the creation of mobile solutions for IBM employees, and providing his expertise in assisting IBM customers with enterprise mobile engagements. His core experiences are in web, security, distributed computing models, and mobile technologies.
Rahul Gupta is an Advisory IT Architect with IBM Global Technology Services (GTS) in the US. He is a Certified Service-Oriented Architecture (SOA) Architect with nine years of professional experience in IBM messaging technologies. At his current assignment, he works as a middleware consultant for various clients in North America. His core experiences are in lab testing, performance tuning, and Level 3 development for IBM Integration Bus and WebSphere MQ. Rahul has been a technical speaker for messaging-related topics at various WebSphere conferences. He is a recognized inventor by the IBM innovation community.
Vasfi Gucer is an IBM Redbooks Project Leader with the IBM International Technical Support Organization. He has more than 18 years of experience in the areas of systems management, networking hardware, and software. He writes extensively and teaches IBM classes worldwide about IBM products. His focus has been on cloud computing for the last three years. Vasfi is also an IBM Certified Senior IT Specialist, Project Management Professional (PMP), IT Infrastructure Library (ITIL) V2 Manager, and ITIL V3 Expert.
Vladimir Kislicins works as a Mobile Developer and Consultant for ISSW Mobile Practice, Europe. His main focus is on the rapid prototype development and proof of concept projects. With a passion for mobile and web technologies, Vladimir has extensive experience with Hybrid mobile, as well as native Android development. Vladimir is co-author of several Prior Art publications that focus on optimizing software processes to reduce battery consumption on mobile devices.
x Building Real-time Mobile Solutions with MQTT and IBM MessageSight Thanks to the following people for their contributions to this project:
Karen Wallace International Technical Support Organization, Raleigh Center
Thanks to the authors of the previous editions of this book: Authors of Building Smarter Planet Solutions with MQTT and IBM WebSphere MQ Telemetr y, SG24-8054: – Valerie Lampkin – Weng Tat Leong – Leonardo Olivera –Sweta Rawat – Nagesh Subrahmanyam – Rong Xiang – Martin Keen
Now you can become a published author, too!
Here’s an opportunity to spotlight your skills, grow your career, and become a published author - all at the same time! Join an ITSO residency project and help write a book in your area of expertise, while honing your experience using leading-edge technologies. Your efforts will help to increase product acceptance and customer satisfaction, as you expand your network of technical contacts and relationships. Residencies run from two to six weeks in length, and you can participate either in person or as a remote resident working from your home base.
Find out more about the residency program, browse the residency index, and apply online at: ibm.com/redbooks/residencies.html
Comments welcome
Your comments are important to us!
We want our books to be as helpful as possible. Send us your comments about this book or other IBM Redbooks publications in one of the following ways: Use the online Contact us review Redbooks form found at: ibm.com/redbooks Send your comments in an email to: [email protected] Mail your comments to: IBM Corporation, International Technical Support Organization Dept. HYTD Mail Station P099 2455 South Road Poughkeepsie, NY 12601-5400
Preface xi Stay connected to IBM Redbooks