Before you go, check out these stories!

0
Hackernoon logoConnecting RabbitMQ with Node JS by@programming

Connecting RabbitMQ with Node JS

Author profile picture

@programmingkiran

Senior Full Stack

RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that Mr. or Ms. Mailperson will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office and a postman.

A normal work queue setup in RabbitMQ looks like:

Work Queues setup in RabbitMQ

RabbitMQ, and messaging in general, uses some jargon.

Producing means nothing more than sending. A program that sends messages is a producer 

A queue is the name for a post box which lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queue is only bound by the host’s memory & disk limits, it’s essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue.

Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages:

Note that the producer, consumer, and broker do not have to reside on the same host; indeed in most applications they don’t. An application can be both a producer and consumer, too.

In this part of the tutorial we’ll write two small programs in Javascript; a producer that sends a single message, and a consumer that receives messages and prints them out.

Step 1 : Installing Rabbit MQ on local machine.

Install Rabbit MQ on local server. Please go through the below url for installation. https://www.rabbitmq.com/install-windows.html

After starting Rabbit MQ server create an instance .
Once you create an instance, click on the “RabbitMQ Manager” button.

Step 1 : Installing Rabbit MQ on local machine.

Install Rabbit MQ on local server. Please go through the below url for installation. https://www.rabbitmq.com/install-windows.html

After starting Rabbit MQ server create an instance .
Once you create an instance, click on the “RabbitMQ Manager” button.

We can also create a queue from code. The queue should be ready in seconds.

Step 2: Writing Node js application for Rabbit MQ


Index.js

Here goes the core part of sending the message to queue. Express framework is used for routing the API for posting the data from postman. Queue's are published to publishqueue funtion in MQService.

MQSevice.js

Here rabbitMQ connection and channels are created, using amqp. Publishe d queue's are consumed and acknowledged.

Post man Request

Stay Tuned!

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!