Connecting RabbitMQ with Node JS by@programming

Connecting RabbitMQ with Node JS

April 1st 2020 8,062 reads
Read on Terminal Reader
Open TLDR
react to story with heart
react to story with light
react to story with boat
react to story with money
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.
image
kiran HackerNoon profile picture

kiran

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:

image

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.

image

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.

image

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.

image

MQSevice.js

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

image

Post man Request

image

Stay Tuned!

react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!