SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Messaging with RabbitMQ and AMQP
Messaging with RabbitMQ and AMQPMessaging with RabbitMQ and AMQP
Messaging with RabbitMQ and AMQPEberhard Wolff
 
실시간 서비스 플랫폼 개발 사례
실시간 서비스 플랫폼 개발 사례실시간 서비스 플랫폼 개발 사례
실시간 서비스 플랫폼 개발 사례John Kim
 
The RabbitMQ Message Broker
The RabbitMQ Message BrokerThe RabbitMQ Message Broker
The RabbitMQ Message BrokerMartin Toshev
 
Introduction To RabbitMQ
Introduction To RabbitMQIntroduction To RabbitMQ
Introduction To RabbitMQKnoldus Inc.
 
ActiveMQ In Action
ActiveMQ In ActionActiveMQ In Action
ActiveMQ In ActionBruce Snyder
 
Rabbitmq an amqp message broker
Rabbitmq an amqp message brokerRabbitmq an amqp message broker
Rabbitmq an amqp message brokerANASYS
 
Introduction to Apache Camel
Introduction to Apache CamelIntroduction to Apache Camel
Introduction to Apache CamelClaus Ibsen
 
IBM MQ Appliance - Administration simplified
IBM MQ Appliance - Administration simplifiedIBM MQ Appliance - Administration simplified
IBM MQ Appliance - Administration simplifiedAnthony Beardsmore
 
SignalR for ASP.NET Developers
SignalR for ASP.NET DevelopersSignalR for ASP.NET Developers
SignalR for ASP.NET DevelopersShivanand Arur
 
IBM MQ Online Tutorials
IBM MQ Online TutorialsIBM MQ Online Tutorials
IBM MQ Online TutorialsBigClasses.com
 
Debugging with Fiddler
Debugging with FiddlerDebugging with Fiddler
Debugging with FiddlerIdo Flatow
 
HATEOAS 101 - Opinionated Introduction to a REST API Style
HATEOAS 101 - Opinionated Introduction to a REST API StyleHATEOAS 101 - Opinionated Introduction to a REST API Style
HATEOAS 101 - Opinionated Introduction to a REST API StyleApigee | Google Cloud
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorialMohammed Fazuluddin
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJSHoang Long
 

Was ist angesagt? (20)

Message Broker System and RabbitMQ
Message Broker System and RabbitMQMessage Broker System and RabbitMQ
Message Broker System and RabbitMQ
 
Messaging with RabbitMQ and AMQP
Messaging with RabbitMQ and AMQPMessaging with RabbitMQ and AMQP
Messaging with RabbitMQ and AMQP
 
실시간 서비스 플랫폼 개발 사례
실시간 서비스 플랫폼 개발 사례실시간 서비스 플랫폼 개발 사례
실시간 서비스 플랫폼 개발 사례
 
Completable future
Completable futureCompletable future
Completable future
 
The RabbitMQ Message Broker
The RabbitMQ Message BrokerThe RabbitMQ Message Broker
The RabbitMQ Message Broker
 
Introduction To RabbitMQ
Introduction To RabbitMQIntroduction To RabbitMQ
Introduction To RabbitMQ
 
ActiveMQ In Action
ActiveMQ In ActionActiveMQ In Action
ActiveMQ In Action
 
CQRS
CQRSCQRS
CQRS
 
Rabbitmq an amqp message broker
Rabbitmq an amqp message brokerRabbitmq an amqp message broker
Rabbitmq an amqp message broker
 
Introduction to Apache Camel
Introduction to Apache CamelIntroduction to Apache Camel
Introduction to Apache Camel
 
IBM MQ Appliance - Administration simplified
IBM MQ Appliance - Administration simplifiedIBM MQ Appliance - Administration simplified
IBM MQ Appliance - Administration simplified
 
SignalR for ASP.NET Developers
SignalR for ASP.NET DevelopersSignalR for ASP.NET Developers
SignalR for ASP.NET Developers
 
Node.js Express Framework
Node.js Express FrameworkNode.js Express Framework
Node.js Express Framework
 
IBM MQ Online Tutorials
IBM MQ Online TutorialsIBM MQ Online Tutorials
IBM MQ Online Tutorials
 
Debugging with Fiddler
Debugging with FiddlerDebugging with Fiddler
Debugging with Fiddler
 
RabbitMQ.ppt
RabbitMQ.pptRabbitMQ.ppt
RabbitMQ.ppt
 
Express node js
Express node jsExpress node js
Express node js
 
HATEOAS 101 - Opinionated Introduction to a REST API Style
HATEOAS 101 - Opinionated Introduction to a REST API StyleHATEOAS 101 - Opinionated Introduction to a REST API Style
HATEOAS 101 - Opinionated Introduction to a REST API Style
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 

Ähnlich wie 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 DevelopersViktor Gamov
 
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 WebPeter Lubbers
 
Tomcat New Evolution
Tomcat New EvolutionTomcat New Evolution
Tomcat New EvolutionAllan Huang
 
Connecting Applications Everywhere with ActiveMQ
Connecting Applications Everywhere with ActiveMQConnecting Applications Everywhere with ActiveMQ
Connecting Applications Everywhere with ActiveMQRob Davies
 
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!jfarcand
 
Lecture 6 Web Sockets
Lecture 6   Web SocketsLecture 6   Web Sockets
Lecture 6 Web SocketsFahad Golra
 
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)Ericom Software
 
Enhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketEnhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketMauricio "Maltron" Leal
 
Programming WebSockets - OSCON 2010
Programming WebSockets - OSCON 2010Programming WebSockets - OSCON 2010
Programming WebSockets - OSCON 2010sullis
 
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 WebsocketsNaresh Chintalcheru
 
Being HAPI! Reverse Proxying on Purpose
Being HAPI! Reverse Proxying on PurposeBeing HAPI! Reverse Proxying on Purpose
Being HAPI! Reverse Proxying on PurposeAman Kohli
 
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 GrizzlyJustin Lee
 
Camel oneactivemq posta-final
Camel oneactivemq posta-finalCamel oneactivemq posta-final
Camel oneactivemq posta-finalChristian Posta
 
Pushing the web — WebSockets
Pushing the web — WebSocketsPushing the web — WebSockets
Pushing the web — WebSocketsRoland M
 
Real-Time with Flowdock
Real-Time with FlowdockReal-Time with Flowdock
Real-Time with FlowdockFlowdock
 
Xke - Introduction to Apache Camel
Xke - Introduction to Apache CamelXke - Introduction to Apache Camel
Xke - Introduction to Apache CamelAlexis Kinsella
 

Ähnlich wie 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
 
Connecting Applications Everywhere with ActiveMQ
Connecting Applications Everywhere with ActiveMQConnecting Applications Everywhere with ActiveMQ
Connecting Applications Everywhere with ActiveMQ
 
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
 

Mehr von dejanb

How is this sausage made
How is this sausage madeHow is this sausage made
How is this sausage madedejanb
 
Messaging for the cloud
Messaging for the cloudMessaging for the cloud
Messaging for the clouddejanb
 
Scaling out eclipse hono
Scaling out eclipse honoScaling out eclipse hono
Scaling out eclipse honodejanb
 
Building Open Source IoT Cloud
Building Open Source IoT CloudBuilding Open Source IoT Cloud
Building Open Source IoT Clouddejanb
 
Messaging for IoT
Messaging for IoTMessaging for IoT
Messaging for IoTdejanb
 
Introduction to ActiveMQ Apollo
Introduction to ActiveMQ ApolloIntroduction to ActiveMQ Apollo
Introduction to ActiveMQ Apollodejanb
 
Deploying FuseMQ with Fuse Fabric
Deploying FuseMQ with Fuse FabricDeploying FuseMQ with Fuse Fabric
Deploying FuseMQ with Fuse Fabricdejanb
 
Advanced messaging with Apache ActiveMQ
Advanced messaging with Apache ActiveMQAdvanced messaging with Apache ActiveMQ
Advanced messaging with Apache ActiveMQdejanb
 
Apache ActiveMQ - Enterprise messaging in action
Apache ActiveMQ - Enterprise messaging in actionApache ActiveMQ - Enterprise messaging in action
Apache ActiveMQ - Enterprise messaging in actiondejanb
 

Mehr von dejanb (9)

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
 
Advanced messaging with Apache ActiveMQ
Advanced messaging with Apache ActiveMQAdvanced messaging with Apache ActiveMQ
Advanced messaging with Apache ActiveMQ
 
Apache ActiveMQ - Enterprise messaging in action
Apache ActiveMQ - Enterprise messaging in actionApache ActiveMQ - Enterprise messaging in action
Apache ActiveMQ - Enterprise messaging in action
 

Kürzlich hochgeladen

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 WorkerThousandEyes
 
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 slidevu2urc
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
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 RobisonAnna Loughnan Colquhoun
 
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 BrazilV3cube
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Kürzlich hochgeladen (20)

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
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
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
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

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