ActiveMQ 5.4: Stomp over Web Sockets

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.xml file and add ws transport, 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.git

    and 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

    http://localhost:8161/demo/stomp-websocket/example/chat

  • 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.

22 comments

  1. 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#.

  2. 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?

  3. Hi Barry,

    it is not configured by default, but you can do that by following the steps described in the post.

  4. 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.

  5. Hi Pawel, we use whatever is available in Jetty. Will check if there are any updates since my initial implementation. Cheers

  6. 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

  7. 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

  8. As with any production deployment we would need the websocket implementation from activemq to be scalable and highly available. Is there any provision for these i.e. does activemq support websocket in cluster ? with failover / loadbalancing support ?

    Also any hints on how to reverse proxy the activemq websocket.. as we wont like to expose the activemq server to outside world.. as per standard topology client should connect to a host in DMZ which in turn connects to activemq in trusted network.

    1. Rajiv, Were you able to find a reverse proxy for the activemq websocket interface? Any suggestions on what works & what does not?
      My IT folks are shaking in their boots with the thought of putting an external address on the message broker.

  9. Hi Rajiv,

    your questions are too broad. ActiveMQ uses Jetty as a web server, so a single broker/web server doesn’t support all this stuff. You need to take a different look at architecting your solution to provide all that. A single broker is not enough

  10. Hi,
    I have ServiceMix 4.4.1 with ActiveMQ 5.5.1 and Jetty 7.4.5
    Will I be able to use run this example on my configuration
    Thanks

  11. Hi,

    I’d like to use Jetty 9.1 as my app server but would like to embed ActiveMQ as a messaging broker. Is this feature still going to work?

    Many thanks!

    -Anqing

  12. Hi Anqing,

    when this feature is used, ActiveMQ is starting it’s own web server internally and exposes web sockets through it. You can’t use this feature with external web server.

  13. Hi there,

    Awesome guide! However, I am having an issue where when I click on the link for the example after starting activemq, I get a 404 error. Any thoughts?

  14. Hi Dejan,

    I have purchased your book just over a month ago and my question, do you have to use STOMP to implement websockets? I’m trying to set up a web page to communicate to a broker using topic with just the basic example provided at:
    http://www.websocket.org/echo.html

    I like the example provided above, it’s simple, but have yet to have it communicate to my local broker. The conf/activemq.xml file has already been modified to accept websockets too.

    Any help is greatly appreciated.

    Curt

Comments are closed.