paint-brush
Connecting RabbitMQ with Node JSby@programming
8,940 reads
8,940 reads

Connecting RabbitMQ with Node JS

by kiranApril 1st, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

RabbitMQ is a message broker that accepts and forwards messages. A queue is the name for a post box which lives inside RabbitMQ. 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. Express framework is used for routing the API for posting the data from RabbitMQ's postman. We can also create a queue from code. The queue should be ready in seconds.

Coin Mentioned

Mention Thumbnail
featured image - Connecting RabbitMQ with Node JS
kiran HackerNoon profile picture

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!