Exchanging messages with the broker directly from the web browser was always interesting use case (and used by many developers). That’s why ActiveMQ was supporting Ajax API for the long time. It uses Jetty Continuations to implement threadless waiting and asynchronous delivery of messages to the web page.
HTML 5 introduced web sockets, as a standardized way to communicate asynchronously with the server from a web page. This is practically an ideal channel for implementing asynchronous messaging for web pages, so it’s no surprise we had it on a todo list for a long time. Also, since JavaScript easily handles text and JSON formatted data, Stomp protocol is a natural choice for the wire protocol to be used over web sockets.
This solution should bring better messaging capabilities to JavaScript clients then simple Ajax API, as implementing Stomp in JavaScript brings much more messaging-oriented API and features such as transactions, for example.
Jeff Mesnil, of JBoss, implemented a nice JavaScript library for doing Stomp over Web Sockets. So it was time to get this off todo list and implement Web Socket support for ActiveMQ. So, in the latest ActiveMQ 5.4 snapshot, you can find new ws (WebSocket) protocol that allows you to exchange message with the broker, using JavaScript client from your browser.
In the rest of this post, I’ll walk you through the steps needed to configure this transport in the broker and run chat example that comes with the Jeff’s library.
There’s nothing spectacular you need to do on the broker side to enable this support:
-
Download the latest ActiveMQ 5.4 snapshot and install it properly.
-
Edit
${ACTIVEMQ_HOME}/conf/activemq.xmlfile and addwstransport, like this<transportConnectors> <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/> <transportConnector name="websocket" uri="ws://0.0.0.0:61614"/> </transportConnectors> -
Get the client library
git clone git://github.com/jmesnil/stomp-websocket.gitand copy it to the
${ACTIVEMQ_HOME}/webapps/demo/folder -
Start ActiveMQ
${ACTIVEMQ_HOME}/bin/activemq -
Now you can see chat example in the demo application
-
Enjoy playing with your new toy :)

WebSocket Chat Demo
One thing worth noting is that web sockets (just as Ajax) implements same origin policy, so you can access only brokers running on the same host as the web application running the client.
Of course, this is an initial implementation of this functionality so your feedback is more then welcome. Before ActiveMQ 5.4 is released, we’ll polish it further and integrate client and example in the ActiveMQ web demo application.