Track your WebSocket messages with Spring 4 and RabbitMQ

Spring 4 introduces WebSocket support with optional support for STOMP as subprotocol and transparent fallback options with SockJS.

When using STOMP over WebSockets a handy built-in message broker takes care of subscriptions and messages, everything in memory. But when it comes to scaling and clustering, going for a full-featured STOMP broker like RabbitMQ is a much better option.

The question is now: how can we track all these websocket messages?

During development is always useful to be able to see your messages. Luckily RabbitMQ has a firehose tracer and useful tracing UI plugin, an extension of the management plugin. Let’s start by enabling the plugin and tracing:

rabbitmq-plugins enable rabbitmq_tracing
rabbitmqctl trace_on [-n node] [-p vhost]

After restarting RabbitMQ, you should see a new tab called Tracing under the Admin menu.

RabbitMQ Tracing Plugin

Add a new trace, and you’ll start tracing your messages in a log file. Message format can be either text or JSON, this is an example of a text message in our log file:

==================================================================
2014-03-17 22:37:16: Message published

Node:         rabbit@localhost
Exchange:     amq.topic
Routing keys: [<<"company-1-status">>]
Properties:   [{<<"headers">>,table,
                [{<<"content-length">>,longstr,<<"37">>}]}]
Payload: 
{"username":"salmar","status":"busy"}

==================================================================
2014-03-17 22:37:16: Message received

Node:         rabbit@localhost
Exchange:     amq.topic
Queue:        amq.gen-LP763HzfjQWpRiVpcD2Tqg
Routing keys: [<<"company-1-status">>]
Properties:   [{<<"headers">>,table,
                [{<<"content-length">>,longstr,<<"37">>}]}]
Payload: 
{"username":"salmar","status":"busy"}