Using Messaging Protocols to Build Mobile and Web Applications - - PowerPoint PPT Presentation

using messaging protocols to build mobile and web
SMART_READER_LITE
LIVE PREVIEW

Using Messaging Protocols to Build Mobile and Web Applications - - PowerPoint PPT Presentation

Using Messaging Protocols to Build Mobile and Web Applications Jeff Mesnil Jeff Mesnil Software Engineer at Red Hat Core developer on WildFly Application Server, lead for its messaging component Developed messaging brokers (HornetQ,


slide-1
SLIDE 1

Using Messaging Protocols to Build Mobile and Web Applications

Jeff Mesnil

slide-2
SLIDE 2

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@gmail.com
  • @jmesnil on Twitter
slide-3
SLIDE 3

Mobile & Web Messaging

Messaging 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
slide-4
SLIDE 4

Summary

  • Messaging Protocols
  • Mobile & Web Devices
  • STOMP
  • MQTT
  • Q & A
slide-5
SLIDE 5

Messaging Concepts

slide-6
SLIDE 6

Messaging Concepts

  • Message
  • Destination
  • Producer (sender / publisher)
  • Consumer (receiver / subscriber)
  • Broker
slide-7
SLIDE 7

Messaging Concepts

  • Loosely coupled
  • Time independence
  • Location independence
  • Strong Contract
  • Destination
  • Message
slide-8
SLIDE 8

Messaging Concepts

Producer Destination

Cluster of brokers

message

slide-9
SLIDE 9

Messaging Concepts

Producer Destination

Cluster of brokers

message

slide-10
SLIDE 10

Messaging Concepts

Producer Consumer Destination Destination

Cluster of brokers

message

slide-11
SLIDE 11

Messaging Concepts

Producer Consumer Destination Destination

Cluster of brokers

message

slide-12
SLIDE 12

Messaging Concepts

Producer Consumer Destination Destination

Cluster of brokers

message

slide-13
SLIDE 13

Messaging Concepts

Producer Consumer Destination Destination

Cluster of brokers

message

slide-14
SLIDE 14

Messaging Concepts

Producer Consumer Destination Destination

Cluster of brokers

message

slide-15
SLIDE 15

Messaging Models

  • How messages will be routed from producers to

consumers

  • Point-to-point
  • Publish/Subscribe
  • Others
slide-16
SLIDE 16

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

message

slide-17
SLIDE 17

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

message

slide-18
SLIDE 18

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

message

slide-19
SLIDE 19

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

slide-20
SLIDE 20

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

message

slide-21
SLIDE 21

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

message

slide-22
SLIDE 22

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

message

slide-23
SLIDE 23

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

slide-24
SLIDE 24

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

message

slide-25
SLIDE 25

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

message

slide-26
SLIDE 26

Point-to-Point Model

Producer Consumer #2 Consumer #3 Consumer #1

Queue Broker

message

slide-27
SLIDE 27

Point-to-Point Model

  • Queues
  • One-to-one model
  • One message produced
  • One message consumed
slide-28
SLIDE 28

Publish/Subscribe Model

Producer Consumer #2 Consumer #3 Consumer #1

Broker

message Topic

slide-29
SLIDE 29

Publish/Subscribe Model

Producer Consumer #2 Consumer #3 Consumer #1

Broker

message Topic

slide-30
SLIDE 30

Publish/Subscribe Model

Producer Consumer #2 Consumer #3 Consumer #1

Broker

message Topic message message

slide-31
SLIDE 31

Publish/Subscribe Model

  • Topics
  • One-to-many model
  • One message produced
  • Many messages consumed
  • Durable subscription
slide-32
SLIDE 32

Message

destination headers body

slide-33
SLIDE 33

Messaging Protocols

  • Entreprise
  • AMQP, JMS (Java), MSMQ
  • First-mile
  • STOMP, MQTT
slide-34
SLIDE 34
  • Many sensors (GPS, motion, health)
  • Always On
  • Always with you

Mobile Devices

slide-35
SLIDE 35

Mobile Devices

  • Battery usage
  • Intermittent network connection
  • Network bandwidth
  • Available memory
slide-36
SLIDE 36

Messaging for Mobile Devices

  • Small network footprint
  • Small memory usage
  • Deal with network failures
slide-37
SLIDE 37

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
slide-38
SLIDE 38

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

slide-39
SLIDE 39

Examples

  • Locations application
  • GPS data, iOS application, Web application
  • Motions application
  • Motion data, iOS application, Web application
slide-40
SLIDE 40

Locations

slide-41
SLIDE 41

Locations

slide-42
SLIDE 42

Motions

slide-43
SLIDE 43

Motions

slide-44
SLIDE 44

STOMP

  • Simple Text Oriented Messaging Protocol
  • http://stomp.github.io
  • Open Source brokers (Apache ActiveMQ, JBoss

HornetQ, RabbitMQ,…)

  • Many client implementations
slide-45
SLIDE 45

STOMP

  • Text-based
  • Inspired by HTTP
  • Messaging model is not specified
slide-46
SLIDE 46

STOMP frame

COMMAND
 header1:value1
 header2:value2
 
 body^@

slide-47
SLIDE 47

STOMP SEND frame

SEND
 destination:/topic/device.XXX.location
 content-type:application/json;charset=utf-8
 content-length:83
 
 {“lng”:-122.032,”lat”: 37.335,”ts”:”2014-03-13T17:19:05+01:00”,”de viceID”:”XXX”}

slide-48
SLIDE 48

STOMP producer

  • CONNECT to a broker (+ receives CONNECTED)
  • SEND message to a destination
  • DISCONNECT from the broker
slide-49
SLIDE 49

STOMP consumer

  • CONNECT to a broker (+ receives CONNECTED)
  • SUBSCRIBE to a destination
  • receive MESSAGEs
  • UNSUBSCRIBE from the destination
  • DISCONNECT from the broker
slide-50
SLIDE 50

Authentication

  • login/passcode in CONNECT
  • => CONNECTED if successful
  • => ERROR (+ socket closed) else
slide-51
SLIDE 51

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

slide-52
SLIDE 52

Message Acknowledgement

  • ack in SUBSCRIBE
  • auto*, client, client-individual
  • ACK/NACK frames for client acknowledgement
slide-53
SLIDE 53

Transactions

  • BEGIN / COMMIT / ABORT + transaction

for transaction boundaries

  • transaction in SEND / ACK frames to

identify the transaction

slide-54
SLIDE 54

Receipt

  • Confirmation that broker has received frames
  • receipt in any frame
  • => RECEIPT + receipt-id sent by the broker

upon reception

slide-55
SLIDE 55

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
slide-56
SLIDE 56

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
slide-57
SLIDE 57

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/
slide-58
SLIDE 58

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]; }];

slide-59
SLIDE 59

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" }]; }];

slide-60
SLIDE 60

stomp.js

  • JavaScript library for Web browsers & node.js
  • Open Source, Apache License 2.0
  • github.com/jmesnil/stomp-websocket
slide-61
SLIDE 61

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(); });

slide-62
SLIDE 62

stomp.js consumer

var 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” } }); });

slide-63
SLIDE 63

MQTT

  • light-weight publish/subscribe messaging

protocol

  • http://mqtt.org
  • Open Source brokers (Apache ActiveMQ,

RabbitMQ, Mosquitto)

  • Many client implementations (Eclipse Paho)
slide-64
SLIDE 64

MQTT

  • Binary protocol
  • Publish/subscribe only (no point-to-point)
  • Internet of Things
  • Public broker at iot.eclipse.org
slide-65
SLIDE 65

MQTT Control Packets

  • CONNECT / CONNACK / DISCONNECT
  • PUBLISH (+ PUBACK / PUBREC /

PUBREL / PUBCOMP)

  • SUBSCRIBE / SUBACK
  • UNSUBSCRIBE / UNSUBACK
  • PINGREQ / PINGRESP
slide-66
SLIDE 66

MQTT Producer

  • CONNECT to the MQTT Broker ( + receives

CONNACK)

  • PUBLISH a message to a topic
  • DISCONNECT
slide-67
SLIDE 67

MQTT Consumer

  • CONNECT to the MQTT Broker ( + receives

CONNACK)

  • SUBSCRIBE to a topic (+ SUBACK)
  • PUBLISH message from topic
  • UNSUBSCRIBE from the topic (+ UNSUBACK)
  • DISCONNECT
slide-68
SLIDE 68

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)

slide-69
SLIDE 69

Quality Of Service (QoS)

  • Guarantee of message delivery
  • At Most Once (x1)
  • At Least Once (x2 packets)
  • Exactly Once (x4 packets)
slide-70
SLIDE 70

Retained Message

  • Last message is retained by the topic and

delivered to new consumers

  • Last Known Good value
slide-71
SLIDE 71

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
slide-72
SLIDE 72

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
slide-73
SLIDE 73

MQTT Features

  • Username / password credentials
  • No error handling (MQTT broker closes the

connection upon error)

  • Heart-beating
slide-74
SLIDE 74

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
slide-75
SLIDE 75

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"); }]; } }];

slide-76
SLIDE 76

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]; } }];

slide-77
SLIDE 77

mqtt.js

  • JavaScript library for Web browsers
  • Open Source, Eclipse Public License 1.0
  • eclipse.org/paho/clients/js/
slide-78
SLIDE 78

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);
 },


  • nFailure: function(failure) {


alert(failure.errorMessage);
 }
 });

slide-79
SLIDE 79

mqtt.js consumer

var 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"); },


  • nFailure: function(failure) {


alert(failure.errorMessage);
 }
 }); // subscription callback client.onMessageArrived = function(message) { console.log(“got message “ + message.payloadString); // Alert from XYZ!! };

slide-80
SLIDE 80

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
slide-81
SLIDE 81

Q & A

slide-82
SLIDE 82

Thank You!

  • jmesnil.net
  • jmesnil@gmail.com
  • @jmesnil