RabbitMQ logo

What is RabbitMQ and when should I use it?

RabbitMQ is a message broker that implements AMPQ (Advance message queuing protocol). It’s open source and provides an easy way to get a messaging service up and running.

Use cases

A huge advantage of RabbitMQ is it reliability. It’s not a server/client relationship where the two must be up for communication to occur. Instead, you can send a message to a queue and the receiver can process it later. The result is a perfectly decoupled system.

For example, after registering as a new user, you should receive a welcome email and other actions should occur using external APIs. With RabbitMQ you can send a message after a signup is completed and the message can be relayed later to your smtp senders and APIs.

Let’s say in the same scenario there’s a high volume of requests on your app. You need to be able to receive and perform multiple actions on each request without your server locking up with processing. In this scenario, a queueing service like RabbitMQ sits between your web service and processing service to ensure a high level of reliability

When not to use RabbitMQ – RabbitMQ is all about reliability. For apps that involve chat or realtime data, a websocket might be a better solution.

Components of RabbitMQ

There are 4 main components involved with RabbitMQ:

  • Producer: Produces message and sends to exchange
  • Consumer: Consumes message
  • Exchange: Receives message form producer and pushes them to the appropriate queues
  • Queue: Receives message from exchange before it is sent to consumer

Exchanges

Think of exchanges as your router. It receive a message from the producer and routes it to the appropriate queue.

In terms of routing, there are 4 ways that RabbitMQ can bind to queues:

  • Fanout: Sends message to all queues
  • Direct: One message is delivered to exactly one queue
  • Topic: Uses defined labels for routing, messages are matched and sent to queues
  • Headers: Routed to queue that matches the message’s header

Creating an exchange and sending a message

const amqp = require(‘amqp/callback_api’);

// connect to RabbitMQ server
amqp.connect(‘amqp://localhost’, (err, conn) => {
  // create channel for communication
  conn.createChannel((err, ch) => {
     // Create exchange
     ch.assertExchange(‘myExchange’, ‘fanout’, {durable: true})
 
     // send message as a buffer
     ch.publish(‘myExchange’, ‘’, new Buffer(‘A message’));
  })
})

Receiving a message

const amqp = require(‘amqp/callback_api’);

// connect to RabbitMQ server
amqp.connect(‘amqp://localhost’, (err, conn) => {
  // create channel for communication
  conn.createChannel((err, ch) => {
     // Create exchange
     ch.assertExchange(‘myExchange’, ‘fanout’, {durable: true})
 
     // create queue
     ch.assertQueue('myQueue', {durable: true}, (err, q) => {
       ch.bindQueue(q.queue, ex, '')
          
       // consume message from queue
       ch.consume(q.queue, function(msg) {
         console.log(msg.content.toString())
       })
     })
  })
})
No Comments

Post a Comment