Introduction

Besides Ajax API, starting with version 5.4.0 onwards, you can use HTML5 WebSockets to exchange messages with the broker from your browser.

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

Configuration

ActiveMQ comes with WebSocket transport which implements Stomp over WebSockets functionality.

To configure it, you need to place something like this to your ActiveMQ configuration file

<transportConnectors>
  <transportConnector name="websocket" uri="ws://0.0.0.0:61614"/>
</transportConnectors>

One thing worth noting is that web sockets (just as Ajax) implements the same origin policy, so you can access only brokers running on the same host as the web application running the client.

Secure Web Sockets

Version 5.7.0 introduced Secure Web Socket transport. To configure it you need two things. First, you need to configure a new transport connector like this

<transportConnectors>
  <transportConnector name="secure_websocket" uri="wss://0.0.0.0:61614"/>
</transportConnectors>

Note that we use wss url prefix to denote a secured version of the protocol. Next you need to provide SSL context for this transport. You can do that by providing sslContext in your broker configuration in a similar fashion as you'd do for ssl or https transports.

<sslContext>
    <sslContext keyStore="file:${activemq.conf}/broker.ks"
                keyStorePassword="password" trustStore="file:${activemq.conf}/broker.ts"
                trustStorePassword="password"
    />
</sslContext>

That's it, your secure websocket transport is ready. Take a look at the next section to see how to use a demo to test it out.

h1.Demo

As of version 5.6.0, an adapted demo of stomp-websocket library is integrated with ActiveMQ web demo application. Enable ws transport, start the broker and go to http://localhost:8161/demo/websocket to see it in action.

Clients

More Resources