RabbitMQ and Node.js Hello World

Getting Started

A basic messaging application consists of 3 parts, the Publisher,the Broker, and the Consumer. The Publisher publishes messages to the broker, the broker decides which consumer will receive the message and the consumer processes the message. I have been exploring different kinds of applications that would help me make a robust messaging application. RabbitMQ is loaded with features that allow you to make all sorts of robust messaging patterns so I decided to use RabbitMQ. The following is an example of a basic application that utilizes RabbitMQ as a broker.



The publisher.js will create messages and publishes them to RabbitMQ to be brokered.

///RabbitMQ nodejs library
var amqp = require('amqp');

///Create a connection to your RabbitMQ
var connection = amqp.createConnection();

///Connection is ready
connection.on('ready', function() {

  console.log('Server Connected');
  var exchange = conn.exchange('order', {
    type: 'topic', ///This is the type of exchange I want to use
    confirm: true
  }, function() {
    console.log('Exchange connected');
    var i = 0;
    ///Simulating messages being published
    setInterval(function() {
      exchange.publish('task_queue', 'Hello World! ' + i, {
        durable: true,
      }, function(res) {
        console.log('confirmed ',res);
    }, 1000);

##Broker ##### RabbitMQ The Broker is going to be RabbitMQ.

Install on MacOSX

brew install rabbitmq

For more detailed instructions follow this link

Then launch the server in your terminal


##Consumer ##### consumer.js Create one more file and call it the consumer.js. Consumer.js will receive the messages that are brokered by RabbitMQ. ``` var amqp = require('amqp'); ///connect to rabbitmq var connection = amqp.createConnection(); ///connection is ready connection.on('ready', function() { ///connect to the queue connection.queue('task_queue', { autoDelete: false, confirm: true }, function(queue) { ///get the exchange connection var exchange = connection.exchange('order', { type: 'topic' }); ///bind the queue to the exchange. THIS IS VERY IMPORTANT queue.bind(exchange, 'task_queue') console.log(' [*] Waiting for messages. To exit press CTRL+C'); ///subscribe to the queue queue.subscribe({ ack: true }, function(msg, cb) { console.log(cb) console.log(" [x] Received %s", msg.data.toString('utf-8')); setTimeout(function() { ///Done processing the message. queue.shift() }, 5000); }); }); }); ```
###Everything Working Together Lets get this application working. Open a terminal and run node against the publisher.js file. ``` node publisher.js ``` If everything was done right, you should see *Hello World* printed incrementally in your terminal. Then in another terminal, run node against the consumer.js file. ``` node consumer.js ``` You should see *[x] Received Hello World* printed incrementally in your terminal. You can also run node against additional consumer.js and you will notice how RabbitMQ redistributes the messages to the new consumers. Pretty cool right?