Software Development

STOMP over WebSocket

STOMP is Simple Text Oriented Messaging Protocol. It defines an interoperable wire format that allows a STOMP client to communicate with any STOMP message broker. This provides easy and widespread messaging interoperability among different languages, platforms and brokers.

The specification defines what makes it different from other messaging protocols:

It is an alternative to other open messaging protocols such as AMQP and implementation specific wire protocols used in JMS brokers such as OpenWire. It distinguishes itself by covering a small subset of commonly used messaging operations rather than providing a comprehensive messaging API.

STOMP is a frame-based protocol. A frame consists of a command, a set of optional headers and an optional body. Commonly used commands are:

  • SEND
  • ACK
  • NACK

WebSocket messages are also transmitted as frames. STOMP over WebSocket maps STOMP frames to WebSocket frames.

Different messaging servers like HornetQ, ActiveMQ, RabbitMQ, and others provide native support for STOMP over WebSocket. Lets take a look at a simple sample on how to use STOMP over WebSocket using ActiveMQ.

The source code for the sample is available at

Lets get started!

  1. Download ActiveMQ 5.10 or provision an ActiveMQ instance in OpenShift as explained at
    workspaces> rhc app-create activemq diy --from-code=git://
    Using diy-0.1 (Do-It-Yourself 0.1) for 'diy'
    Application Options
    Domain:      milestogo
    Cartridges:  diy-0.1
    Source Code: git://
    Gear Size:   default
    Scaling:     no
    Creating application 'activemq' ... done
      Disclaimer: This is an experimental cartridge that provides a way to try unsupported languages, frameworks, and middleware on OpenShift.
    Waiting for your DNS name to be available ... done
    Cloning into 'activemq'...
    Warning: Permanently added the RSA host key for IP address '' to the list of known hosts.
    Your application 'activemq' is now available.
      SSH to:
      Git remote: ssh://
      Cloned to:  /Users/arungupta/workspaces/activemq
    Run 'rhc show-app activemq' for more details about your app.
    workspaces> rhc port-forward activemq
    Checking available ports ... done
    Forwarding ports ...
    To connect to a service running on OpenShift, use the Local address
    Service Local                OpenShift
    ------- --------------- ---- -----------------
    java   =>
    java   =>
    java  =>
    java  =>
    java  =>
    java   =>
    Press CTRL-C to terminate port forwarding
  2. Download WildFly 8.1 zip, unzip, and start as bin/
  3. Clone the repo and deploy the sample on WildFly:
    git clone
    cd wildfly-samples
    mvn wildfly:deploy
  4. Access the application at localhost:8080/websocket-stomp-1.0-SNAPSHOT/ to see the page as:
  5. Specify text payload “foobar and usse ActiveMQ conventions for topics and queues to specify a queue name as “/queue/myQ1″. Click on Connect, Send Message, Subscribe, and Disconnect buttons one after the other. This will display messages on your browser window where WebSocket connection is established, STOMP message is sent to the queue, subscribed to the queue to receive the message, and then finally disconnected.STOMP frames can be seen using Chrome Developer Tools as shown:
    techtip53-websocket-framesAs you can see, each STOMP frame is mapped to a WebSocket frame.

In short, ActiveMQ on OpenShift is running a STOMP broker on port 61614 and is accessible on localhost:61614 by port-forwarding. Clicking on Connect button uses the Stomp library bundled with the application to establish a WebSocket connection with ws://localhost:61614/. Subsequent buttons send STOMP frames over WebSocket as shown in the Frames tab of Developer Tools.

Read more details about how all the pieces work together at Jeff has also written an excellent book explaining STOMP over WebSocket and lot more other interesting things that can be done over WebSocket in his Mobile and Web Messaging book.

Reference: STOMP over WebSocket from our JCG partner Arun Gupta at the Miles to go 2.0 … blog.

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
9 years ago

Wildfly and ActiveMQ? Sound superfluous for at least one JMS system here…

Dang Trung Nguyen
Dang Trung Nguyen
7 years ago

Hi there,

I have a problem with my android application connect to stomp server.
If could, Can I have a simple example for both android – stomp server – websocket client demo.


Back to top button