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 = connection.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)
connection.close()

Credits and more explanation: https://www.rabbitmq.com/tutorials/tutorial-three-python.html

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';  
console.log(client);  
client.subscribe(mq_exchange, on_message);
}
function on_connect_error() 
{  
output.innerHTML += 'Connection failed!';
}
function on_message(m) {  
console.log('message received');   
console.log(m);  
output.innerHTML += m.body + '';
}
var client = Stomp.client(mq_url);
window.onload = function () {
  output = document.getElementById("output");
  client.connect(    
  mq_username,    
  mq_password,    
  on_connect,    
  on_connect_error,    
  mq_vhost  
  );
}
Credits and more explaination: http://koo.fi/blog/2013/02/18/web-messaging-with-rabbitmq-web-stomp-and-sockjs/ 

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.

Comments