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

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.

Three Sources to get paid "likes" for your Facebook page

Almost every blogger and webmaster uses social media to get visitors on their websites and blogs. Facebook is one of the largest social media traffic resource. While many webmasters believe in making quality content and attract visitors to their site, many try to get a strong start up base for their websites. There are many services on the internet which provide fans and followers to your social media pages in exchange of some amount of money. Here are three of them you can use to get Facebook likes.

10 most common logo design mistakes

Designing a logo or getting a logo designed nowadays is quite multifaceted and difficult. As the technology is progressing and advancing each day. So is the logo designing business, the competition is getting more complex by each passing day and so is the designing. Although there are numerous tools and techniques which can be utilised to design auspicious and pertinent logo designs for the companies and brands.While designing a logo most of the designers make some very stupid and careless mistakes. And these mistakes lead them to devastation, there are numerous mistakes done by the designers although they are quite small but highly affects the whole look of the design. In logo designing very minuet details are noticed and thus one has to be very careful while designing a logo. 1. Using the wrong typography There are heaps of websites to download fonts at no cost however be responsive of the latent consequences in terms of authorisation as well as rights, which might leave you having…