SlideShare a Scribd company logo
1 of 54
CamelOne 2013
June 10-11 2013
Boston, MA
Messaging for web and
mobile with Apache
ActiveMQ
By Bosanac Dejan
1
CamelOne 2013
CamelOne
2
Bosanac Dejan?
• Senior Sofware Engineer at RedHat
• Apache ActiveMQ committer and PMC member
• Co-author of ActiveMQ in Action
• Blog – http://sensatic.net
• Twitter – http://twitter.com/dejanb
CamelOne 2013
CamelOne
Agenda
• Challenges of web messaging
• REST vs Stomp
• In-browser messaging (Ajax vs Web Sockets)
• Mobile messaging using MQTT
• Striking the balance
3
CamelOne 2013
CamelOne
Messaging for Web
• Connect from any web application backend
(Ruby, PHP, Python, …)
• Connect directly from the browser (AJAX, Web
Sockets)
• The main requirement is simplicity
4
CamelOne 2013
CamelOne
What’s wrong with Http?
• Nothing at all!
• Ideal for simple request-reply communication
• Lacks semantics for publish-subscribe and
point-to-point communication
5
CamelOne 2013
CamelOne
Limitations
• Pull based protocol
• Easy simple producing
• There’s no concept of consumer or subscription
• There’s no concept of transactions
6
CamelOne 2013
CamelOne
Pull Consuming
• HTTP techniques
• Long polling
• Comet
• Maintain a state
• Session
• ClientID
7
CamelOne 2013
CamelOne
Push Consuming
• Web Hooks – http://webhooks.org
• Provide a callback (HTTP URL) to be called on
event
• Trigger callback on every message
8
CamelOne 2013
CamelOne Camel HTTP
component
9
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring”>
<route>
<from uri="activemq:topic:events"/>
<to uri="http://mysite.com/events"/>
</route>
</camelContext>
• HawtIO – http://hawt.io
• Missing API for dynamically managing
subscribers
CamelOne 2013
CamelOne
Stomp – what it is?
• http://stomp.github.com
• Simple Text Orientated Messaging Protocol
• HTTP for the messaging realm
10
CamelOne 2013
CamelOne
Stomp – basics
• Very simple, so it’s easy to write clients and
servers in practically any language
• A lot of client APIs in C, Java, Ruby, Pyhton, JS,
PHP
• Implemented by ActiveMQ, Apollo, HornetQ,
RabbitMQ
11
CamelOne 2013
CamelOne
Stomp - Protocol
• Text based headers,
similar to HTTP
• Can transport binary
bodies
• Frame command for
every messaging
concept, like
CONNECT, MESSAGE,
SUBSCRIBE, ACK, etc.
12
MESSAGE
subscription:0
message-id:007
destination:/queue/a
content-type:text/plain
hello queue a^@
CamelOne 2013
CamelOne
Stomp + ActiveMQ
• Available transports
• NIO implementation for better scalability
• SSL for secure communication
13
<transportConnectors>
<transportConnector name=”stomp" uri=”stomp://0.0.0.0:61613"/>
<transportConnector name=”stomp+nio" uri=”stomp+nio://0.0.0.0:61614"/>
<transportConnector name=”stomp+ssl" uri=”stomp+ssl://0.0.0.0:61615"/>
<transportConnector name=”stomp+nio+ssl"
uri=”stomp+nio+ssl://0.0.0.0:61615"/>
</transportConnectors>
CamelOne 2013
CamelOne
Stomp Java Client
• StompJMS -
https://github.com/fusesource/stompjms
• APIs:
• JMS
• Blocking
• Future
• Callback
14
CamelOne 2013
CamelOne
15
Stomp stomp = new Stomp("localhost", 61613);
Future<FutureConnection> future = stomp.connectFuture();
FutureConnection connection = future.await();
CONNECT
host:localhost
accept-version:1.1
CONNECTED
heart-beat:0,0
session:ID:vidra.local-56933-1369046267671-2:1
server:ActiveMQ/5.9-SNAPSHOT
version:1.1
CamelOne 2013
CamelOne
16
StompFrame frame = new StompFrame(SEND);
frame.addHeader(DESTINATION, StompFrame.encodeHeader("/queue/test"));
frame.addHeader(MESSAGE_ID, StompFrame.encodeHeader("test"));
frame.content(new Buffer("Important Message".getBytes("UTF-8")));
Future<Void> sendFuture = connection.send(frame);
sendFuture.await();
SEND
message-id:test
destination:/queue/test
content-length:17
Important Message
CamelOne 2013
CamelOne
17
StompFrame disconnect = new StompFrame(DISCONNECT);
Future<Void> disconnectFuture = connection.send(disconnect);
disconnectFuture.await();
DISCONNECT
CamelOne 2013
CamelOne
18
Stomp stomp = new Stomp("localhost", 61613);
Future<FutureConnection> future = stomp.connectFuture();
FutureConnection connection = future.await();
CONNECT
host:localhost
accept-version:1.1
CONNECTED
heart-beat:0,0
session:ID:vidra.local-56933-1369046267671-2:1
server:ActiveMQ/5.9-SNAPSHOT
version:1.1
CamelOne 2013
CamelOne
19
Future<StompFrame> receiveFuture = connection.receive();
StompFrame frame = new StompFrame(SUBSCRIBE);
frame.addHeader(DESTINATION, StompFrame.encodeHeader("/queue/test"));
AsciiBuffer id = connection.nextId();
frame.addHeader(ID, id);
Future<StompFrame> response = connection.request(frame);
response.await();
SUBSCRIBE
receipt:2
destination:/queue/test
id:1
RECEIPT
receipt-id:2
CamelOne 2013
CamelOne
20
StompFrame received = receiveFuture.await();
System.out.println(received.content());
MESSAGE
message-id:ID:vidra.local-56933-1369046267671-2:1:-1:1:1
destination:/queue/test
timestamp:1369046474700
expires:0
subscription:1
content-length:17
priority:4
Important Message
CamelOne 2013
CamelOne
21
StompFrame unsubscribe = new StompFrame(UNSUBSCRIBE);
unsubscribe.addHeader(ID, id);
Future<Void> unsubscribeFuture = connection.send(unsubscribe);
unsubscribeFuture.await();
UNSUBSCRIBE
id:1
CamelOne 2013
CamelOne
22
StompFrame disconnect = new StompFrame(DISCONNECT);
Future<Void> disconnectFuture = connection.send(disconnect);
disconnectFuture.await();
DISCONNECT
CamelOne 2013
CamelOne
Advanced Stomp
• Ack modes
• Transactions
• Reliable messaging
• Protocol Negotiations
• Heart-beating
23
CamelOne 2013
CamelOne
Stomp and ActiveMQ
• Queues and Topics
• Reliable Messaging
• Temporary destinations
• Durable topic subscribers
• Destination wildcards
• Message selectors
24
CamelOne 2013
CamelOne
Stomp and ActiveMQ
• Message expiration
• Composite destinations
• Priority consumers
• Exclusive consumers
25
CamelOne 2013
CamelOne
In-browser Messaging
• Use JavaScript to produce and consume
messages directly from the browser
• We need to leverage existing web technologies
like Ajax and Web Sockets
• We need a web server that’s able to
communicate with the broker
26
CamelOne 2013
CamelOne
Ajax
• Old-school way
• Comes bundled with ActiveMQ distribution
27
CamelOne 2013
CamelOne
Ajax – explained
• Requires additional servlet as an intermediary
between broker and clients
• POST to send messages
• Jetty continuations to receive messages
28
CamelOne 2013
CamelOne
Ajax – Example
29
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/amq_jquery_adapter.js"></script>
<script type="text/javascript" src="js/amq.js"></script>
<script type="text/javascript">
var amq = org.activemq.Amq;
amq.init({
uri: 'amq',
logging: true,
timeout: 20
});
</script>
CamelOne 2013
CamelOne
Ajax – Example
30
amq.sendMessage(”queue://TEST”, “Important Message!”);
var myHandler =
{
rcvMessage: function(message)
{
console.log(“Received message: ” + message);
}
};
amq.addListener(“myListener”, “queue://TEST”, myHandler.rcvMessage);
CamelOne 2013
CamelOne
WebSocket
• Evolution over Ajax and Comet
• Defines a “socket” – permanent duplex
connection – between browser and server
• Server and browser can exchange messages
31
CamelOne 2013
CamelOne
WebSocket
• Fully standardized and part of HTML5 spec
• Protocol – standardized by IETF
• API – standardized by W3C
• Supported by most modern web servers and
browsers
32
CamelOne 2013
CamelOne
WebSocket Example
33
var connection = new WebSocket("ws://localhost:8161");
connection.onopen = function() {
console.log("Connection opened!");
};
connection.onmessage = function(msg) {
console.log("Received Message: " + msg.data);
};
connection.onerror = function(error) {
console.log("Error occured: " + error);
}
connection.onclose = function(evt) {
console.log("Connection closed!");
};
connection.send("Important Message!");
connection.close();
CamelOne 2013
CamelOne
WebSocket + ActiveMQ
• WebSocket is a plain socket – like a raw TCP
• We need a protocol on top of it to use all
concepts of messaging and connect to broker
• WebSocket+Stomp ideal combination!
34
CamelOne 2013
CamelOne
WebSocket + ActiveMQ
• New ws and wss transports
• wss transport needs SSL context configuration
35
<transportConnectors>
<transportConnector name="websocket" uri="ws://0.0.0.0:61613"/>
<transportConnector name="secure_websocket" uri="wss://0.0.0.0:61614"/>
</transportConnectors>
CamelOne 2013
CamelOne
stomp-websocket
• Client side library stomp-websocket
• http://github.com/jmesnil/stomp-websocket
• Supports Stomp 1.1
• Not a “pure” Stomp as it requires WebSocket
handshake
36
CamelOne 2013
CamelOne stomp-websocket
Example
37
var client = Stomp.client("ws://localhost:61614");
var connected = false;
client.connect("admin", "admin", function() {
connected = true;
client.subscribe("/queue/test", function(message) {
console.log("Received message " + message);
}
});
if (connected) {
client.send("/queue/test", {priority: 9}, "Important Message!");
}
if (connected) {
client.disconnect();
}
CamelOne 2013
CamelOne
Messaging for Mobile
• Different set of requirements
• Low bandwidth network
• Small footprint
• Low power usage
38
CamelOne 2013
CamelOne
MQTT
• http://mqtt.org/ - MQ Telemetry Transport
• IoT (Internet of Things) protocol
• Efficient binary protocol
• Developed by IBM for embedded devices
telemetry
39
CamelOne 2013
CamelOne
MQTT Features
• Low bandwidth
• Smallest frame 2 bytes
• Unreliable networks
• Small footprint
40
CamelOne 2013
CamelOne
MQTT for mobile
• Efficient battery usage - Power Profiliing: MQTT
on Android -
http://stephendnicholas.com/archives/219
• Ideal for native mobile applications
• Usecase: Facebook messanger
• Phone-to-phone delivery in milliseconds, rather than
seconds
• Without killing battery life
41
CamelOne 2013
CamelOne
MQTT
• Publish/subscribe protocol – topics only
• 3 QoS Options:
• At Most Once – message loss might occur
• At Least Once – duplicates might occur
• Exactly Once – guaranteed delivery
42
CamelOne 2013
CamelOne
MQTT + ActiveMQ
• Available transports
• NIO implementation for better scalability
• SSL for secure communication
43
<transportConnectors>
<transportConnector name=”mqtt" uri=”mqtt://0.0.0.0:1883"/>
<transportConnector name=”mqtt+nio" uri=”mqtt+nio://0.0.0.0:1884"/>
<transportConnector name=”mqtt+ssl" uri=”mqtt+ssl://0.0.0.0:1885"/>
<transportConnector name=”mqtt+nio+ssl"
uri=”mqtt+nio+ssl://0.0.0.0:1886"/>
</transportConnectors>
CamelOne 2013
CamelOne
MQTT client
• mqtt-client https://github.com/fusesource/mqtt-
client
• APIs:
• Blocking
• Callback
• Future
44
CamelOne 2013
CamelOne
MQTT Example
45
MQTT mqtt = new MQTT();
mqtt.setHost("localhost", 1883);
final CallbackConnection connection = mqtt.callbackConnection();
CamelOne 2013
CamelOne
MQTT Example
46
connection.connect(new Callback<Void>() {
public void onSuccess(Void value) {
connection.publish("test",
"Important Message!".getBytes(),
QoS.AT_LEAST_ONCE,
false,
null
);
}
public void onFailure(Throwable value) {
connection.disconnect(null);
}
});
CamelOne 2013
CamelOne
MQTT Example
47
final Promise<Buffer> result = new Promise<Buffer>();
connection.listener(new Listener() {
public void onConnected() {}
public void onDisconnected() {}
public void onPublish(UTF8Buffer topic, Buffer body,
Runnable ack) {
result.onSuccess(body);
ack.run();
}
public void onFailure(Throwable value) {
result.onFailure(value);
connection.disconnect(null);
}
});
LOG.info("Received: " + result.await(5, TimeUnit.MINUTES));
CamelOne 2013
CamelOne
MQTT Example
48
connection.connect(new Callback<Void>() {
public void onSuccess(Void aVoid) {
Topic[] topics = {
new Topic(utf8("test"), QoS.AT_LEAST_ONCE)
};
connection.subscribe(topics, null);
}
public void onFailure(Throwable value) {
connection.disconnect(null);
}
});
CamelOne 2013
CamelOne
MQTT Android Example
• https://github.com/jsherman1/android-mqtt-
demo/
49
CamelOne 2013
CamelOne
Striking the Balance
• Lots of possibilities, how to choose right?
• Native mobile apps should consider MQTT
• Do you need live updates in your browser?
• WebSockets ideal for HTML5 apps with limited
number of users that needs instant update
• For everyone else, there's backend messaging
50
CamelOne 2013
CamelOne
Stomp pitfall
• Short-lived connections
• Every page view, open a new connection to the
broker
• Puts heavy load on the broker
• Eliminates all advance messaging mechanisms
– message prefetches, producer flow control,
etc.
51
CamelOne 2013
CamelOne
Stomp configuration
52
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry queue=">" producerFlowControl="false">
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<transportConnectors>
<transportConnector name="stomp+nio"
uri="stomp+nio://0.0.0.0:61613?transport.closeAsync=false"/>
</transportConnectors>
CamelOne 2013
CamelOne
Conclusion
• Messaging is not the thing of the enterprise
anymore
• Things want to get integrated
• We have technology to do that TODAY!
53
CamelOne 2013
CamelOne
AMA
• Links
• Stomp - http://stomp.github.com
• https://github.com/fusesource/stompjms
• MQTT – http://mqtt.org
• https://github.com/fusesource/mqtt-client
• Blog: http://sensatic.net
• Twitter: http://twitter.com/dejanb
54

More Related Content

What's hot

Atomicity In Redis: Thomas Hunter
Atomicity In Redis: Thomas HunterAtomicity In Redis: Thomas Hunter
Atomicity In Redis: Thomas Hunter
Redis Labs
 
ActiveMQ In Action
ActiveMQ In ActionActiveMQ In Action
ActiveMQ In Action
Bruce Snyder
 
Spring Batch 2.0
Spring Batch 2.0Spring Batch 2.0
Spring Batch 2.0
Guido Schmutz
 

What's hot (20)

2021.laravelconf.tw.slides1
2021.laravelconf.tw.slides12021.laravelconf.tw.slides1
2021.laravelconf.tw.slides1
 
Atomicity In Redis: Thomas Hunter
Atomicity In Redis: Thomas HunterAtomicity In Redis: Thomas Hunter
Atomicity In Redis: Thomas Hunter
 
Spring batch
Spring batchSpring batch
Spring batch
 
What Are Coroutines In Kotlin?
What Are Coroutines In Kotlin?What Are Coroutines In Kotlin?
What Are Coroutines In Kotlin?
 
Spring batch introduction
Spring batch introductionSpring batch introduction
Spring batch introduction
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UK
 
GraalVM
GraalVMGraalVM
GraalVM
 
ActiveMQ In Action
ActiveMQ In ActionActiveMQ In Action
ActiveMQ In Action
 
Saga pattern and event sourcing with kafka
Saga pattern and event sourcing with kafkaSaga pattern and event sourcing with kafka
Saga pattern and event sourcing with kafka
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
NATS Connect Live | SwimOS & NATS
NATS Connect Live | SwimOS & NATSNATS Connect Live | SwimOS & NATS
NATS Connect Live | SwimOS & NATS
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
 
gRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at SquaregRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at Square
 
Kafka PPT.pptx
Kafka PPT.pptxKafka PPT.pptx
Kafka PPT.pptx
 
GraalVM Overview Compact version
GraalVM Overview Compact versionGraalVM Overview Compact version
GraalVM Overview Compact version
 
From Generator to Fiber the Road to Coroutine in PHP
From Generator to Fiber the Road to Coroutine in PHPFrom Generator to Fiber the Road to Coroutine in PHP
From Generator to Fiber the Road to Coroutine in PHP
 
Practical non blocking microservices in java 8
Practical non blocking microservices in java 8Practical non blocking microservices in java 8
Practical non blocking microservices in java 8
 
Real Time Communication using Node.js and Socket.io
Real Time Communication using Node.js and Socket.ioReal Time Communication using Node.js and Socket.io
Real Time Communication using Node.js and Socket.io
 
Spring Batch 2.0
Spring Batch 2.0Spring Batch 2.0
Spring Batch 2.0
 
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
 

Similar to Messaging for Web and Mobile with Apache ActiveMQ

WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
Viktor Gamov
 
Tomcat New Evolution
Tomcat New EvolutionTomcat New Evolution
Tomcat New Evolution
Allan Huang
 
Programming WebSockets - OSCON 2010
Programming WebSockets - OSCON 2010Programming WebSockets - OSCON 2010
Programming WebSockets - OSCON 2010
sullis
 
Camel oneactivemq posta-final
Camel oneactivemq posta-finalCamel oneactivemq posta-final
Camel oneactivemq posta-final
Christian Posta
 
Pushing the web — WebSockets
Pushing the web — WebSocketsPushing the web — WebSockets
Pushing the web — WebSockets
Roland M
 

Similar to Messaging for Web and Mobile with Apache ActiveMQ (20)

WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
 
HTML5 WebSocket: The New Network Stack for the Web
HTML5 WebSocket: The New Network Stack for the WebHTML5 WebSocket: The New Network Stack for the Web
HTML5 WebSocket: The New Network Stack for the Web
 
Tomcat New Evolution
Tomcat New EvolutionTomcat New Evolution
Tomcat New Evolution
 
Websockets on the JVM: Atmosphere to the rescue!
Websockets on the JVM: Atmosphere to the rescue!Websockets on the JVM: Atmosphere to the rescue!
Websockets on the JVM: Atmosphere to the rescue!
 
Lecture 6 Web Sockets
Lecture 6   Web SocketsLecture 6   Web Sockets
Lecture 6 Web Sockets
 
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
 
The HTML5 WebSocket API
The HTML5 WebSocket APIThe HTML5 WebSocket API
The HTML5 WebSocket API
 
WebSockets with Spring 4
WebSockets with Spring 4WebSockets with Spring 4
WebSockets with Spring 4
 
Enhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketEnhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocket
 
Programming WebSockets - OSCON 2010
Programming WebSockets - OSCON 2010Programming WebSockets - OSCON 2010
Programming WebSockets - OSCON 2010
 
Building Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using WebsocketsBuilding Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using Websockets
 
Websocket
WebsocketWebsocket
Websocket
 
Being HAPI! Reverse Proxying on Purpose
Being HAPI! Reverse Proxying on PurposeBeing HAPI! Reverse Proxying on Purpose
Being HAPI! Reverse Proxying on Purpose
 
Building Websocket Applications with GlassFish and Grizzly
Building Websocket Applications with GlassFish and GrizzlyBuilding Websocket Applications with GlassFish and Grizzly
Building Websocket Applications with GlassFish and Grizzly
 
Websocket
WebsocketWebsocket
Websocket
 
Camel oneactivemq posta-final
Camel oneactivemq posta-finalCamel oneactivemq posta-final
Camel oneactivemq posta-final
 
Pushing the web — WebSockets
Pushing the web — WebSocketsPushing the web — WebSockets
Pushing the web — WebSockets
 
Real-Time with Flowdock
Real-Time with FlowdockReal-Time with Flowdock
Real-Time with Flowdock
 
Xke - Introduction to Apache Camel
Xke - Introduction to Apache CamelXke - Introduction to Apache Camel
Xke - Introduction to Apache Camel
 
Js remote conf
Js remote confJs remote conf
Js remote conf
 

More from dejanb

Scaling out eclipse hono
Scaling out eclipse honoScaling out eclipse hono
Scaling out eclipse hono
dejanb
 
Apache ActiveMQ - Enterprise messaging in action
Apache ActiveMQ - Enterprise messaging in actionApache ActiveMQ - Enterprise messaging in action
Apache ActiveMQ - Enterprise messaging in action
dejanb
 

More from dejanb (8)

How is this sausage made
How is this sausage madeHow is this sausage made
How is this sausage made
 
Messaging for the cloud
Messaging for the cloudMessaging for the cloud
Messaging for the cloud
 
Scaling out eclipse hono
Scaling out eclipse honoScaling out eclipse hono
Scaling out eclipse hono
 
Building Open Source IoT Cloud
Building Open Source IoT CloudBuilding Open Source IoT Cloud
Building Open Source IoT Cloud
 
Messaging for IoT
Messaging for IoTMessaging for IoT
Messaging for IoT
 
Introduction to ActiveMQ Apollo
Introduction to ActiveMQ ApolloIntroduction to ActiveMQ Apollo
Introduction to ActiveMQ Apollo
 
Deploying FuseMQ with Fuse Fabric
Deploying FuseMQ with Fuse FabricDeploying FuseMQ with Fuse Fabric
Deploying FuseMQ with Fuse Fabric
 
Apache ActiveMQ - Enterprise messaging in action
Apache ActiveMQ - Enterprise messaging in actionApache ActiveMQ - Enterprise messaging in action
Apache ActiveMQ - Enterprise messaging in action
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

Messaging for Web and Mobile with Apache ActiveMQ

  • 1. CamelOne 2013 June 10-11 2013 Boston, MA Messaging for web and mobile with Apache ActiveMQ By Bosanac Dejan 1
  • 2. CamelOne 2013 CamelOne 2 Bosanac Dejan? • Senior Sofware Engineer at RedHat • Apache ActiveMQ committer and PMC member • Co-author of ActiveMQ in Action • Blog – http://sensatic.net • Twitter – http://twitter.com/dejanb
  • 3. CamelOne 2013 CamelOne Agenda • Challenges of web messaging • REST vs Stomp • In-browser messaging (Ajax vs Web Sockets) • Mobile messaging using MQTT • Striking the balance 3
  • 4. CamelOne 2013 CamelOne Messaging for Web • Connect from any web application backend (Ruby, PHP, Python, …) • Connect directly from the browser (AJAX, Web Sockets) • The main requirement is simplicity 4
  • 5. CamelOne 2013 CamelOne What’s wrong with Http? • Nothing at all! • Ideal for simple request-reply communication • Lacks semantics for publish-subscribe and point-to-point communication 5
  • 6. CamelOne 2013 CamelOne Limitations • Pull based protocol • Easy simple producing • There’s no concept of consumer or subscription • There’s no concept of transactions 6
  • 7. CamelOne 2013 CamelOne Pull Consuming • HTTP techniques • Long polling • Comet • Maintain a state • Session • ClientID 7
  • 8. CamelOne 2013 CamelOne Push Consuming • Web Hooks – http://webhooks.org • Provide a callback (HTTP URL) to be called on event • Trigger callback on every message 8
  • 9. CamelOne 2013 CamelOne Camel HTTP component 9 <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring”> <route> <from uri="activemq:topic:events"/> <to uri="http://mysite.com/events"/> </route> </camelContext> • HawtIO – http://hawt.io • Missing API for dynamically managing subscribers
  • 10. CamelOne 2013 CamelOne Stomp – what it is? • http://stomp.github.com • Simple Text Orientated Messaging Protocol • HTTP for the messaging realm 10
  • 11. CamelOne 2013 CamelOne Stomp – basics • Very simple, so it’s easy to write clients and servers in practically any language • A lot of client APIs in C, Java, Ruby, Pyhton, JS, PHP • Implemented by ActiveMQ, Apollo, HornetQ, RabbitMQ 11
  • 12. CamelOne 2013 CamelOne Stomp - Protocol • Text based headers, similar to HTTP • Can transport binary bodies • Frame command for every messaging concept, like CONNECT, MESSAGE, SUBSCRIBE, ACK, etc. 12 MESSAGE subscription:0 message-id:007 destination:/queue/a content-type:text/plain hello queue a^@
  • 13. CamelOne 2013 CamelOne Stomp + ActiveMQ • Available transports • NIO implementation for better scalability • SSL for secure communication 13 <transportConnectors> <transportConnector name=”stomp" uri=”stomp://0.0.0.0:61613"/> <transportConnector name=”stomp+nio" uri=”stomp+nio://0.0.0.0:61614"/> <transportConnector name=”stomp+ssl" uri=”stomp+ssl://0.0.0.0:61615"/> <transportConnector name=”stomp+nio+ssl" uri=”stomp+nio+ssl://0.0.0.0:61615"/> </transportConnectors>
  • 14. CamelOne 2013 CamelOne Stomp Java Client • StompJMS - https://github.com/fusesource/stompjms • APIs: • JMS • Blocking • Future • Callback 14
  • 15. CamelOne 2013 CamelOne 15 Stomp stomp = new Stomp("localhost", 61613); Future<FutureConnection> future = stomp.connectFuture(); FutureConnection connection = future.await(); CONNECT host:localhost accept-version:1.1 CONNECTED heart-beat:0,0 session:ID:vidra.local-56933-1369046267671-2:1 server:ActiveMQ/5.9-SNAPSHOT version:1.1
  • 16. CamelOne 2013 CamelOne 16 StompFrame frame = new StompFrame(SEND); frame.addHeader(DESTINATION, StompFrame.encodeHeader("/queue/test")); frame.addHeader(MESSAGE_ID, StompFrame.encodeHeader("test")); frame.content(new Buffer("Important Message".getBytes("UTF-8"))); Future<Void> sendFuture = connection.send(frame); sendFuture.await(); SEND message-id:test destination:/queue/test content-length:17 Important Message
  • 17. CamelOne 2013 CamelOne 17 StompFrame disconnect = new StompFrame(DISCONNECT); Future<Void> disconnectFuture = connection.send(disconnect); disconnectFuture.await(); DISCONNECT
  • 18. CamelOne 2013 CamelOne 18 Stomp stomp = new Stomp("localhost", 61613); Future<FutureConnection> future = stomp.connectFuture(); FutureConnection connection = future.await(); CONNECT host:localhost accept-version:1.1 CONNECTED heart-beat:0,0 session:ID:vidra.local-56933-1369046267671-2:1 server:ActiveMQ/5.9-SNAPSHOT version:1.1
  • 19. CamelOne 2013 CamelOne 19 Future<StompFrame> receiveFuture = connection.receive(); StompFrame frame = new StompFrame(SUBSCRIBE); frame.addHeader(DESTINATION, StompFrame.encodeHeader("/queue/test")); AsciiBuffer id = connection.nextId(); frame.addHeader(ID, id); Future<StompFrame> response = connection.request(frame); response.await(); SUBSCRIBE receipt:2 destination:/queue/test id:1 RECEIPT receipt-id:2
  • 20. CamelOne 2013 CamelOne 20 StompFrame received = receiveFuture.await(); System.out.println(received.content()); MESSAGE message-id:ID:vidra.local-56933-1369046267671-2:1:-1:1:1 destination:/queue/test timestamp:1369046474700 expires:0 subscription:1 content-length:17 priority:4 Important Message
  • 21. CamelOne 2013 CamelOne 21 StompFrame unsubscribe = new StompFrame(UNSUBSCRIBE); unsubscribe.addHeader(ID, id); Future<Void> unsubscribeFuture = connection.send(unsubscribe); unsubscribeFuture.await(); UNSUBSCRIBE id:1
  • 22. CamelOne 2013 CamelOne 22 StompFrame disconnect = new StompFrame(DISCONNECT); Future<Void> disconnectFuture = connection.send(disconnect); disconnectFuture.await(); DISCONNECT
  • 23. CamelOne 2013 CamelOne Advanced Stomp • Ack modes • Transactions • Reliable messaging • Protocol Negotiations • Heart-beating 23
  • 24. CamelOne 2013 CamelOne Stomp and ActiveMQ • Queues and Topics • Reliable Messaging • Temporary destinations • Durable topic subscribers • Destination wildcards • Message selectors 24
  • 25. CamelOne 2013 CamelOne Stomp and ActiveMQ • Message expiration • Composite destinations • Priority consumers • Exclusive consumers 25
  • 26. CamelOne 2013 CamelOne In-browser Messaging • Use JavaScript to produce and consume messages directly from the browser • We need to leverage existing web technologies like Ajax and Web Sockets • We need a web server that’s able to communicate with the broker 26
  • 27. CamelOne 2013 CamelOne Ajax • Old-school way • Comes bundled with ActiveMQ distribution 27
  • 28. CamelOne 2013 CamelOne Ajax – explained • Requires additional servlet as an intermediary between broker and clients • POST to send messages • Jetty continuations to receive messages 28
  • 29. CamelOne 2013 CamelOne Ajax – Example 29 <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="js/amq_jquery_adapter.js"></script> <script type="text/javascript" src="js/amq.js"></script> <script type="text/javascript"> var amq = org.activemq.Amq; amq.init({ uri: 'amq', logging: true, timeout: 20 }); </script>
  • 30. CamelOne 2013 CamelOne Ajax – Example 30 amq.sendMessage(”queue://TEST”, “Important Message!”); var myHandler = { rcvMessage: function(message) { console.log(“Received message: ” + message); } }; amq.addListener(“myListener”, “queue://TEST”, myHandler.rcvMessage);
  • 31. CamelOne 2013 CamelOne WebSocket • Evolution over Ajax and Comet • Defines a “socket” – permanent duplex connection – between browser and server • Server and browser can exchange messages 31
  • 32. CamelOne 2013 CamelOne WebSocket • Fully standardized and part of HTML5 spec • Protocol – standardized by IETF • API – standardized by W3C • Supported by most modern web servers and browsers 32
  • 33. CamelOne 2013 CamelOne WebSocket Example 33 var connection = new WebSocket("ws://localhost:8161"); connection.onopen = function() { console.log("Connection opened!"); }; connection.onmessage = function(msg) { console.log("Received Message: " + msg.data); }; connection.onerror = function(error) { console.log("Error occured: " + error); } connection.onclose = function(evt) { console.log("Connection closed!"); }; connection.send("Important Message!"); connection.close();
  • 34. CamelOne 2013 CamelOne WebSocket + ActiveMQ • WebSocket is a plain socket – like a raw TCP • We need a protocol on top of it to use all concepts of messaging and connect to broker • WebSocket+Stomp ideal combination! 34
  • 35. CamelOne 2013 CamelOne WebSocket + ActiveMQ • New ws and wss transports • wss transport needs SSL context configuration 35 <transportConnectors> <transportConnector name="websocket" uri="ws://0.0.0.0:61613"/> <transportConnector name="secure_websocket" uri="wss://0.0.0.0:61614"/> </transportConnectors>
  • 36. CamelOne 2013 CamelOne stomp-websocket • Client side library stomp-websocket • http://github.com/jmesnil/stomp-websocket • Supports Stomp 1.1 • Not a “pure” Stomp as it requires WebSocket handshake 36
  • 37. CamelOne 2013 CamelOne stomp-websocket Example 37 var client = Stomp.client("ws://localhost:61614"); var connected = false; client.connect("admin", "admin", function() { connected = true; client.subscribe("/queue/test", function(message) { console.log("Received message " + message); } }); if (connected) { client.send("/queue/test", {priority: 9}, "Important Message!"); } if (connected) { client.disconnect(); }
  • 38. CamelOne 2013 CamelOne Messaging for Mobile • Different set of requirements • Low bandwidth network • Small footprint • Low power usage 38
  • 39. CamelOne 2013 CamelOne MQTT • http://mqtt.org/ - MQ Telemetry Transport • IoT (Internet of Things) protocol • Efficient binary protocol • Developed by IBM for embedded devices telemetry 39
  • 40. CamelOne 2013 CamelOne MQTT Features • Low bandwidth • Smallest frame 2 bytes • Unreliable networks • Small footprint 40
  • 41. CamelOne 2013 CamelOne MQTT for mobile • Efficient battery usage - Power Profiliing: MQTT on Android - http://stephendnicholas.com/archives/219 • Ideal for native mobile applications • Usecase: Facebook messanger • Phone-to-phone delivery in milliseconds, rather than seconds • Without killing battery life 41
  • 42. CamelOne 2013 CamelOne MQTT • Publish/subscribe protocol – topics only • 3 QoS Options: • At Most Once – message loss might occur • At Least Once – duplicates might occur • Exactly Once – guaranteed delivery 42
  • 43. CamelOne 2013 CamelOne MQTT + ActiveMQ • Available transports • NIO implementation for better scalability • SSL for secure communication 43 <transportConnectors> <transportConnector name=”mqtt" uri=”mqtt://0.0.0.0:1883"/> <transportConnector name=”mqtt+nio" uri=”mqtt+nio://0.0.0.0:1884"/> <transportConnector name=”mqtt+ssl" uri=”mqtt+ssl://0.0.0.0:1885"/> <transportConnector name=”mqtt+nio+ssl" uri=”mqtt+nio+ssl://0.0.0.0:1886"/> </transportConnectors>
  • 44. CamelOne 2013 CamelOne MQTT client • mqtt-client https://github.com/fusesource/mqtt- client • APIs: • Blocking • Callback • Future 44
  • 45. CamelOne 2013 CamelOne MQTT Example 45 MQTT mqtt = new MQTT(); mqtt.setHost("localhost", 1883); final CallbackConnection connection = mqtt.callbackConnection();
  • 46. CamelOne 2013 CamelOne MQTT Example 46 connection.connect(new Callback<Void>() { public void onSuccess(Void value) { connection.publish("test", "Important Message!".getBytes(), QoS.AT_LEAST_ONCE, false, null ); } public void onFailure(Throwable value) { connection.disconnect(null); } });
  • 47. CamelOne 2013 CamelOne MQTT Example 47 final Promise<Buffer> result = new Promise<Buffer>(); connection.listener(new Listener() { public void onConnected() {} public void onDisconnected() {} public void onPublish(UTF8Buffer topic, Buffer body, Runnable ack) { result.onSuccess(body); ack.run(); } public void onFailure(Throwable value) { result.onFailure(value); connection.disconnect(null); } }); LOG.info("Received: " + result.await(5, TimeUnit.MINUTES));
  • 48. CamelOne 2013 CamelOne MQTT Example 48 connection.connect(new Callback<Void>() { public void onSuccess(Void aVoid) { Topic[] topics = { new Topic(utf8("test"), QoS.AT_LEAST_ONCE) }; connection.subscribe(topics, null); } public void onFailure(Throwable value) { connection.disconnect(null); } });
  • 49. CamelOne 2013 CamelOne MQTT Android Example • https://github.com/jsherman1/android-mqtt- demo/ 49
  • 50. CamelOne 2013 CamelOne Striking the Balance • Lots of possibilities, how to choose right? • Native mobile apps should consider MQTT • Do you need live updates in your browser? • WebSockets ideal for HTML5 apps with limited number of users that needs instant update • For everyone else, there's backend messaging 50
  • 51. CamelOne 2013 CamelOne Stomp pitfall • Short-lived connections • Every page view, open a new connection to the broker • Puts heavy load on the broker • Eliminates all advance messaging mechanisms – message prefetches, producer flow control, etc. 51
  • 52. CamelOne 2013 CamelOne Stomp configuration 52 <destinationPolicy> <policyMap> <policyEntries> <policyEntry queue=">" producerFlowControl="false"> </policyEntry> </policyEntries> </policyMap> </destinationPolicy> <transportConnectors> <transportConnector name="stomp+nio" uri="stomp+nio://0.0.0.0:61613?transport.closeAsync=false"/> </transportConnectors>
  • 53. CamelOne 2013 CamelOne Conclusion • Messaging is not the thing of the enterprise anymore • Things want to get integrated • We have technology to do that TODAY! 53
  • 54. CamelOne 2013 CamelOne AMA • Links • Stomp - http://stomp.github.com • https://github.com/fusesource/stompjms • MQTT – http://mqtt.org • https://github.com/fusesource/mqtt-client • Blog: http://sensatic.net • Twitter: http://twitter.com/dejanb 54