Skip to main content

Receiving Fanout Messages on SockJS Subscribers in RabbitMQ

SockJS is a popular library for receiving and sending messages from messaging queues in Javascript which works on Stomp over Websockets. The basic underlying architecture of RabbitMQ consists of Exchanges which receive messages and distribute them to respective queues. One message in general is received by one consumer. Sometimes there is a need to send (or broadcast) messages to multiple consumers. In this case, the Publish/Subscribe mechanism of RabbitMQ can be utilized.

This tutorial focuses on creating a RabbitMQ publisher in Python using the Pika library and consuming the messages in a SockJS client. Since this we will be consuming messages directly from the Exchange, any number of consumers can be created and they will all receive the messages.

Creating a Publisher in Python

The following code creates a connection to RabbitMQ server on localhost, creates a new exchange named 'broadcast' and publishes a message 'Hello World!' to it.

#!/usr/bin/env python
import pika
import sysimport os
url = os.environ.get('CLOUDAMQP_URL', 'amqp://localhost')
params = pika.URLParameters(url)params.socket_timeout = 5
connection = pika.BlockingConnection(params) 
channel =
channel.exchange_declare(exchange='broadcast', type='fanout')
message = 'Hello World!'
channel.basic_publish(exchange='broadcast', routing_key='', body=message)
print(" [x] Sent %r" % message)

Credits and more explanation:

The exchange is of type fanout, which means that it will broadcast messages to all the consumers. The routing_key is kept blank because we don't want to publish this message in any particular queue but the exchange.

Creating Consumers in JavaScript using SockJS

The following code connects to a SockJS service running on localhost and consumes messages from the broadcast exchange.

Stomp.WebSocketClass = SockJS;
var mq_username = "guest",    
mq_password = "guest",    
mq_vhost    = "/",    
mq_url      = 'http://' + window.location.hostname + ':15674/stomp',
    mq_exchange = "/exchange/broadcast";
var output; 
function on_connect() {  
output.innerHTML += 'Connected to RabbitMQ-Web-Stomp';  
client.subscribe(mq_exchange, on_message);
function on_connect_error() 
output.innerHTML += 'Connection failed!';
function on_message(m) {  
console.log('message received');   
output.innerHTML += m.body + '';
var client = Stomp.client(mq_url);
window.onload = function () {
  output = document.getElementById("output");
Credits and more explaination: 

You can create any number of such consumers and they all will be receiving the sent messages.

This tutorial was brought to you by WeboGraffiti. Thanks for reading.


Popular posts from this blog

The Top 10 WeboGraffiti Posts From 2012

2012 had been a great year for WeboGraffiti. Some major events we had were WeboGraffiti Photography Campaign, our first giveaway, shifting from WordPress to Blogger and many more. We had fun this year and we hope 2013 to bring more happiness to our website. This post is a compilation of the 10 posts on WeboGraffiti which were the most popular, most read and most commented throughout the year. We hope you will like these posts.

3 Problems After Moving WordPress Host and Their Solutions

Many WordPress users think a million times before shifting to a new web host, server or a new domain because of the fear of loosing their data, visitors or search rank. To some extent, their fear is genuine because a lot of problems may be caused after WordPress server migration which may eventually result into data loss, page load errors and even site rendering. In this post are three common problems which comes up after host migration and some effective ways to solve them.

Is Your Sony SmartWatch Not Charging?

Though the Sony SmartWatch 1 was the first of its kind and its users may consider it an excellent gadget, one problem that many users faced with it was that it does not charge if the charger is not used for a while. I faced the same issue and found some unexpected solutions of this problem which I have complied in this post.

The Pin Scratch Method This might seem funny but it worked for me. Take something which is softly pointed; such as a guitar pick or a paper pin and gently rub the four small golden circles with it. Do not use much force to do this as you may damage your charger. After scratching, try connecting the charger again and see if it works. You might see wonders.

This problem might occur when your charging slots get covered with dust particles. The solution would work most of the times. The Paper Clip Method If the first method doesn't work for you, the problem might be in the locking system of your watch and charger. The charging area of your Sony SmartWatch has fou…