Last week I attended the Con-Fess conference in Vienna to speak about WebSocket (and its JSF integration). The first demo described how to bring UDP-based network traffic to the browser. Another demo from my presentation showed how to bring JMS to the browser, by using WebSocket.
You can try this out as well – The steps are quite simple:
- Download the JMS Edition of the Kaazing gateway and extract it
- Start the included Apache ActiveMQ server
- Start the gateway itself
- Start the demo stock ticker feed
...
var stompConnectionFactory =
new StompConnectionFactory( "ws://localhost:8000/jms");
// create a connection, by using a JS callback
var connectionFuture =
stompConnectionFactory.createConnection(user, passwd, function () {
if (!connectionFuture.exception) {
connection = connectionFuture.getValue();
// register a JMS exception handler
connection.setExceptionListener(handleException);
// create session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// create a topic
topic = session.createTopic("/topic/destination");
// creating some consumers!
var consumer1 = session.createConsumer(topic);
// attach the callback that receives the stock prices:
consumer1.setMessageListener(handleMessageCallback);
// start!
connection.start(someCallback);
}
....
});
- we receive the actual connection
- we setup a JMS exception handler
- we create a session, a JMS topic and a consumer
- we register a handler for the incoming JMS messages (handleMessageCallback)
- we finally start the connection’s delivery of incoming messages
function handleMessageCallack(message) {
// did Apache ActiveMQ send us a JMS TextMessage?
if (message instanceof TextMessage) {
var body = message.getText();
// DHTML code to show stock values...
}
}
The above JavaScript code is pretty much readable for every JMS coder. The big news is that the JMS communication from the ActiveMQ to the browser (and vice versa) happens over WebSocket. This dramatically reduces overhead and latency for your web application!
The WebSocket gateway is not limited to Stomp/JMS, it has support for other protocols like AMQP, or even XMPP as well!
If you want to try (or see) this stock ticker, you can! It is part of the Kaazing JMS demo bundle. Once you installed the gateway, you can see the stockticker on your machine…
Have fun!
