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

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.
Tweet
Christopher Hunt
Nice post. How about implementing Openwire from Javascript? Wouldn’t that be more efficient? I’d really like to understand why STOMP instead of Openwire. Thanks.
Dejan Bosanac
Hi Christopher,
STOMP is a protocol that is exactly created for this kind of tasks. As it is text-based and simple, it can be practically implemented in any environment. OpenWire on the other hand is a complex binary protocol, and while it is not impossible to implement it in languages such as JavaScript or Ruby, it’s more suited to compiled environments, such as Java or C#.
Barry Kaplan
Christopher, In the latest 5.4-SNAPSHOT (as of yesterday) it seems that ws is not configured. Are there some other steps that are missing from above?
Dejan Bosanac
Hi Barry,
it is not configured by default, but you can do that by following the steps described in the post.
Paweł
It seems that ActiveMQ doesn’t support the newer protocol version, 76, does it? I’ve tested it against Firefox 4.0b and it doesn work there. Still ok with Chrome. Can’t wait for an appropriate update.
Dejan Bosanac
Hi Pawel, we use whatever is available in Jetty. Will check if there are any updates since my initial implementation. Cheers
Bobby
GWT integration?
Ronnie
I need to know what I’m doing wrong….Seems like an ActiveMQ BUG
following the directions here http://www.nighttale.net/activemq/activemq-54-stomp-over-web-sockets.html
1) I’ve downloaded the latest 5.5 server.
2) Downloaded stomp-websocket from https://github.com/jmesnil/stomp-websocket
4) copied stomp-websocket to the ${ACTIVEMQ_HOME}/webapps/demo/ folder
3) in activemq config added:
4) restarted server
5) http://localhost:8161/demo/stomp-websocket/test/index.html
SERVER ERRORS OUT WITH:
| /stomp
java.lang.NullPointerException
at org.apache.activemq.transport.ws.StompServlet.doGet(StompServlet.java
:51)
using Google Chrome 14.0.835.186 m
Dejan Bosanac
Hi Ronnie,
I guess there were some changes in web socket support in recent versions. I suggest you to file a bug in ActiveMQ Jira for this.
Thanks,
Dejan