WebSockets

Connectivity > WebSockets

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. Since JavaScript easily handles text and JSON formatted data, Stomp protocol is a good choice for the wire protocol to be used over web sockets. Since version 5.9.0, we also support efficient binary MQTT protocol over Web sockets.

This solution should bring better messaging capabilities to JavaScript clients then simple Ajax API, as implementing Stomp or MQTT in JavaScript brings much more messaging-oriented API and features such as transactions, for example.

Configuration

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

To configure it, you need to place something like this to your ActiveMQ Classic 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.

Demos

As of version 5.6.0, an adapted demo of stomp-websocket library is integrated with ActiveMQ Classic web demo application.
As of version 5.9.0, we have a similar demo using MQTT and Eclipse Paho client

To see demos:

Clients

More Resources

Apache, ActiveMQ, Apache ActiveMQ, the Apache feather logo, and the Apache ActiveMQ project logo are trademarks of The Apache Software Foundation. Copyright © 2024, The Apache Software Foundation. Licensed under Apache License 2.0.