Connecting RabbitMQ with Node JS

Written by programming | Published 2020/04/01
Tech Story Tags: message-queue | nodejs | mq | rabbitmq | javascript | real-time-communication | beginners | coding

TLDR 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.via the TL;DR App

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!

Written by programming | Senior Full Stack
Published by HackerNoon on 2020/04/01