Using Messaging Protocols to Build Mobile and Web...
Transcript of Using Messaging Protocols to Build Mobile and Web...
![Page 1: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/1.jpg)
Using Messaging Protocols to Build Mobile
and Web ApplicationsJeff Mesnil
![Page 2: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/2.jpg)
Jeff Mesnil• Software Engineer at Red Hat
• Core developer on WildFly Application Server, lead for its messaging component
• Developed messaging brokers (HornetQ, JBoss Messaging, JORAM)
• http://jmesnil.net/
• @jmesnil on Twitter
![Page 3: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/3.jpg)
Mobile & Web MessagingMessaging Protocols for Web and Mobile Devices
http://shop.oreilly.com/product/0636920032366.do
http://mobile-web-messaging.net/
• Published by O'Reilly Media
• Released in August 2014
![Page 4: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/4.jpg)
Summary
• Messaging Protocols
• Mobile & Web Devices
• STOMP
• MQTT
• Q & A
![Page 5: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/5.jpg)
Messaging Concepts
![Page 6: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/6.jpg)
Messaging Concepts
• Message
• Destination
• Producer (sender / publisher)
• Consumer (receiver / subscriber)
• Broker
![Page 7: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/7.jpg)
Messaging Concepts
• Loosely coupled
• Time independence
• Location independence
• Strong Contract
• Destination
• Message
![Page 8: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/8.jpg)
Messaging Concepts
Producer Destination
Cluster of brokers
message
![Page 9: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/9.jpg)
Messaging Concepts
Producer Destination
Cluster of brokers
message
![Page 10: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/10.jpg)
Messaging Concepts
Producer ConsumerDestination Destination
Cluster of brokers
message
![Page 11: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/11.jpg)
Messaging Concepts
Producer ConsumerDestination Destination
Cluster of brokers
message
![Page 12: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/12.jpg)
Messaging Concepts
Producer ConsumerDestination Destination
Cluster of brokers
message
![Page 13: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/13.jpg)
Messaging Concepts
Producer ConsumerDestination Destination
Cluster of brokers
message
![Page 14: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/14.jpg)
Messaging Concepts
Producer ConsumerDestination Destination
Cluster of brokers
message
![Page 15: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/15.jpg)
Messaging Models
• How messages will be routed from producers to consumers
• Point-to-point
• Publish/Subscribe
• Others
![Page 16: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/16.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Brokermessage
![Page 17: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/17.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Broker
message
![Page 18: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/18.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Broker
message
![Page 19: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/19.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Broker
![Page 20: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/20.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Brokermessage
![Page 21: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/21.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Broker
message
![Page 22: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/22.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Broker
message
![Page 23: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/23.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Broker
![Page 24: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/24.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Brokermessage
![Page 25: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/25.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Broker
message
![Page 26: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/26.jpg)
Point-to-Point Model
Producer Consumer #2
Consumer #3
Consumer #1
Queue
Broker
message
![Page 27: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/27.jpg)
Point-to-Point Model
• Queues
• One-to-one model
• One message produced
• One message consumed
![Page 28: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/28.jpg)
Publish/Subscribe Model
Producer Consumer #2
Consumer #3
Consumer #1Broker
message
Topic
![Page 29: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/29.jpg)
Publish/Subscribe Model
Producer Consumer #2
Consumer #3
Consumer #1Brokermessage
Topic
![Page 30: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/30.jpg)
Publish/Subscribe Model
Producer Consumer #2
Consumer #3
Consumer #1Broker
message
Topic
message
message
![Page 31: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/31.jpg)
Publish/Subscribe Model
• Topics
• One-to-many model
• One message produced
• Many messages consumed
• Durable subscription
![Page 32: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/32.jpg)
Message
destination
headers
body
![Page 33: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/33.jpg)
Messaging Protocols
• Entreprise
• AMQP, JMS (Java), MSMQ
• First-mile
• STOMP, MQTT
![Page 34: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/34.jpg)
• Many sensors (GPS, motion, health)
• Always On
• Always with you
Mobile Devices
![Page 35: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/35.jpg)
Mobile Devices
• Battery usage
• Intermittent network connection
• Network bandwidth
• Available memory
![Page 36: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/36.jpg)
Messaging for Mobile Devices
• Small network footprint
• Small memory usage
• Deal with network failures
![Page 37: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/37.jpg)
Web Browsers• “Old” Web browser communication model
• Browser initiates the request, server replies
• No “right” way to push data from server to browser
• HTTP long polling or streaming (RFC 6202)
• Frequent HTTP requests
![Page 38: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/38.jpg)
Web Browsers
• HTML5 Web Sockets
• TCP socket for the Web
• Protocol + JavaScript API
• Enabler for messaging protocols and pushing data from the server to the browser
![Page 39: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/39.jpg)
Examples
• Locations application
• GPS data, iOS application, Web application
• Motions application
• Motion data, iOS application, Web application
![Page 40: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/40.jpg)
Locations
![Page 41: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/41.jpg)
Locations
![Page 42: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/42.jpg)
Motions
![Page 43: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/43.jpg)
Motions
![Page 44: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/44.jpg)
STOMP
• Simple Text Oriented Messaging Protocol
• http://stomp.github.io
• Open Source brokers (Apache ActiveMQ, JBoss HornetQ, RabbitMQ,…)
• Many client implementations
![Page 45: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/45.jpg)
STOMP
• Text-based
• Inspired by HTTP
• Messaging model is not specified
![Page 46: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/46.jpg)
STOMP frame
COMMANDheader1:value1header2:value2body^@
![Page 47: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/47.jpg)
STOMP SEND frame
SENDdestination:/topic/device.XXX.locationcontent-type:application/json;charset=utf-8content-length:83{“lng”:-122.032,”lat”:37.335,”ts”:”2014-03-13T17:19:05+01:00”,”deviceID”:”XXX”}
![Page 48: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/48.jpg)
STOMP producer
• CONNECT to a broker (+ receives CONNECTED)
• SEND message to a destination
• DISCONNECT from the broker
![Page 49: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/49.jpg)
STOMP consumer
• CONNECT to a broker (+ receives CONNECTED)
• SUBSCRIBE to a destination
• receive MESSAGEs
• UNSUBSCRIBE from the destination
• DISCONNECT from the broker
![Page 50: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/50.jpg)
Authentication
• login/passcode in CONNECT
• => CONNECTED if successful
• => ERROR (+ socket closed) else
![Page 51: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/51.jpg)
Heart-Beating• heartbeat negotiation in CONNECT/CONNECTED
• client-to-broker
• broker-to-client
• Connection is considered dead after failing to receive heart-beat during 2x the negotiated value
![Page 52: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/52.jpg)
Message Acknowledgement
• ack in SUBSCRIBE
• auto*, client, client-individual
• ACK/NACK frames for client acknowledgement
![Page 53: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/53.jpg)
Transactions
• BEGIN / COMMIT / ABORT + transaction for transaction boundaries
• transaction in SEND / ACK frames to identify the transaction
![Page 54: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/54.jpg)
Receipt
• Confirmation that broker has received frames
• receipt in any frame
• => RECEIPT + receipt-id sent by the broker upon reception
![Page 55: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/55.jpg)
Error Handling
• ERROR sent by the broker (+ connection closed)
• message contains the description of the error
• Body may contain more information
• No standard status code
![Page 56: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/56.jpg)
STOMP extensions
• Persistence: SEND with persistent:true
• Filtered consumer: SUBSCRIBE with selector: "country in (‘FR’, ‘DE’, ‘IT’)"
• Priority: SEND with priority:7
• Expiration: SEND with expires:1415631438
![Page 57: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/57.jpg)
StompKit
• Objective-C library (iOS, OS X)
• Event-driven (Grand Central Dispatch + Blocks)
• Open Source, Apache License 2.0
• github.com/mobile-web-messaging/StompKit/
![Page 58: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/58.jpg)
StompKit producer// create the client STOMPClient *client = [[STOMPClient alloc] initWithHost:@“192.168.1.25” port:61613]; // connect to the broker [client connectWithLogin:@"mylogin" passcode:@"mypassword" completionHandler:^(STOMPFrame *_, NSError *error) { if (err) { NSLog(@"%@", error); return; }
// send a message [client sendTo:@"/queue/myqueue" body:@"Hello, iOS!"]; // and disconnect [client disconnect]; }];
![Page 59: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/59.jpg)
StompKit consumer// create the client STOMPClient *client = [[STOMPClient alloc] initWithHost:@“192.168.1.25” port:61613]; // connect to the broker [client connectWithLogin:@"mylogin" passcode:@"mypassword" completionHandler:^(STOMPFrame *_, NSError *error) { if (err) { NSLog(@"%@", error); return; }
// subscribe to the destination [client subscribeTo:@"/queue/myqueue" headers:@{} messageHandler:^(STOMPMessage *message) { // called back when the client receive a message // for the /queue/myqueue destination NSLog(@"got message %@", message.body); // => "Hello, iOS" }];
}];
![Page 60: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/60.jpg)
stomp.js
• JavaScript library for Web browsers & node.js
• Open Source, Apache License 2.0
• github.com/jmesnil/stomp-websocket
![Page 61: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/61.jpg)
stomp.js producer
var url = "ws://localhost:61614/stomp";var client = Stomp.client(url);
client.connect("mylogin", "mypasscode", function(frame) { // upon successful connection var data = { “deviceID”: “XYZ” } // JSON object
client.send("/queue/myqueue", {}, // no headers JSON.stringify(data));
client.disconnect(); });
![Page 62: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/62.jpg)
stomp.js consumervar url = "ws://localhost:61614/stomp";var client = Stomp.client(url);
client.connect("mylogin", "mypasscode", function(frame) { // upon successful connection
client.subscribe("/queue/myqueue", function(message) { // called back when the client receive a message // for the /queue/myqueue destination var data = JSON.parse(message.body); // => { “deviceID”: “XYZ” } });
});
![Page 63: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/63.jpg)
MQTT
• light-weight publish/subscribe messaging protocol
• http://mqtt.org
• Open Source brokers (Apache ActiveMQ, RabbitMQ, Mosquitto)
• Many client implementations (Eclipse Paho)
![Page 64: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/64.jpg)
MQTT
• Binary protocol
• Publish/subscribe only (no point-to-point)
• Internet of Things
• Public broker at iot.eclipse.org
![Page 65: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/65.jpg)
MQTT Control Packets• CONNECT / CONNACK / DISCONNECT
• PUBLISH (+ PUBACK / PUBREC / PUBREL / PUBCOMP)
• SUBSCRIBE / SUBACK
• UNSUBSCRIBE / UNSUBACK
• PINGREQ / PINGRESP
![Page 66: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/66.jpg)
MQTT Producer
• CONNECT to the MQTT Broker ( + receives CONNACK)
• PUBLISH a message to a topic
• DISCONNECT
![Page 67: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/67.jpg)
MQTT Consumer• CONNECT to the MQTT Broker ( + receives CONNACK)
• SUBSCRIBE to a topic (+ SUBACK)
• PUBLISH message from topic
• UNSUBSCRIBE from the topic (+ UNSUBACK)
• DISCONNECT
![Page 68: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/68.jpg)
Topic Wildcards• / (topic level separator)
• /mwm/XYZ/alerts
• # (multilevel wildcard)
• /mwm/# (will match /mwm/XYZ/alerts)
• + (single level wildcard)
• /mwm/+/alerts (will match /mwm/XYZ/alerts but not /mwm/XYZ/data)
![Page 69: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/69.jpg)
Quality Of Service (QoS)
• Guarantee of message delivery
• At Most Once (x1)
• At Least Once (x2 packets)
• Exactly Once (x4 packets)
![Page 70: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/70.jpg)
Retained Message
• Last message is retained by the topic and delivered to new consumers
• Last Known Good value
![Page 71: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/71.jpg)
Last Will
• Broker will publish a Last Will message on a Last Will topic on behalf of a client in case of unexpected disconnection
• Setup by the client in CONNECT
• Monitor liveliness of clients
![Page 72: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/72.jpg)
Clean Session
• Whether a broker stores any client state between its connections
• Setup by the client in CONNECT based on the client identifier
• Required for reliable messaging
• Memory and administration overhead
![Page 73: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/73.jpg)
MQTT Features
• Username / password credentials
• No error handling (MQTT broker closes the connection upon error)
• Heart-beating
![Page 74: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/74.jpg)
MQTTKit
• Objective-C library (iOS, OS X)
• Event-driven (Grand Central Dispatch + Blocks)
• Open Source, Apache License 2.0
• github.com/mobile-web-messaging/MQTTKit
![Page 75: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/75.jpg)
MQTTKit producer// create the client with a unique client ID NSString *clientID = ... MQTTClient *client = [[MQTTClient alloc] initWithClientId:clientID];
// connect to the MQTT server [self.client connectToHost:@"iot.eclipse.org" completionHandler:^(NSUInteger code) { if (code == ConnectionAccepted) { // when the client is connected, send a MQTT message [self.client publishString:@"Hello, MQTT" toTopic:@"/MQTTKit/example" withQos:AtMostOnce retain:NO completionHandler:^(int mid) { NSLog(@"message has been delivered"); }]; } }];
![Page 76: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/76.jpg)
MQTTKit consumer// create the client with a unique client ID NSString *clientID = ... MQTTClient *client = [[MQTTClient alloc] initWithClientId:clientID];
// define the handler that will be called when MQTT messages are //received by the client [client setMessageHandler:^(MQTTMessage *message) { NSString *text = [message.payloadString]; NSLog(@"received message %@", text); }];
// connect the MQTT client [client connectToHost:@"iot.eclipse.org" completionHandler:^(MQTTConnectionReturnCode code) { if (code == ConnectionAccepted) { // when the client is connected, subscribe to the topic // to receive message. [self.client subscribe:@"/MQTTKit/example" withCompletionHandler:nil]; } }];
![Page 77: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/77.jpg)
mqtt.js
• JavaScript library for Web browsers
• Open Source, Eclipse Public License 1.0
• eclipse.org/paho/clients/js/
![Page 78: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/78.jpg)
mqtt.js producer
var clientID = Math.random().toString(12);var client = new Messaging.Client(“iot.eclipse.org", 80, clientID);client.connect({onSuccess: function(frame) { // this function is executed after a successful connection to // the MQTT broker var message = new Messaging.Message(“Alert from XYZ!!”); message.destinationName = “/mwm/XYZ/alerts”; client.send(message); }, onFailure: function(failure) { alert(failure.errorMessage); }});
![Page 79: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/79.jpg)
mqtt.js consumervar clientID = Math.random().toString(12);var client = new Messaging.Client(“iot.eclipse.org", 80, clientID);client.connect({onSuccess: function(frame) { // once the client is successfully connected, // subscribe to all the mwm topics client.subscribe("/mwm/+/alerts"); }, onFailure: function(failure) { alert(failure.errorMessage); }});
// subscription callback client.onMessageArrived = function(message) { console.log(“got message “ + message.payloadString); // Alert from XYZ!! };
![Page 80: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/80.jpg)
Conclusion
• Messaging to complement request/reply protocols (HTTP)
• MQTT & STOMP as first-mile protocols (from device/browser to broker)
• Mobile devices and modern Web browsers
• Internet of Things
![Page 81: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/81.jpg)
Q & A
![Page 82: Using Messaging Protocols to Build Mobile and Web Applicationsmedia.jmesnil.net/slides/2014-11-13_oreilly_webcast.pdf · Using Messaging Protocols to Build Mobile and Web Applications](https://reader034.fdocuments.us/reader034/viewer/2022052518/5f0dad6f7e708231d43b89e0/html5/thumbnails/82.jpg)
Thank You!
• jmesnil.net
• @jmesnil