Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
42 views
12 pages
RAbbitMQ in Node
how to use rabbitmq in node
Uploaded by
Cornel Manescu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save RAbbitMQ in Node For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
42 views
12 pages
RAbbitMQ in Node
how to use rabbitmq in node
Uploaded by
Cornel Manescu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save RAbbitMQ in Node For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save RAbbitMQ in Node For Later
You are on page 1
/ 12
Search
Fullscreen
Open inapp 7 Q_ Search Medium é@ Sharmila S Jan 28,2022 - 6 minread - © Listen Wsve vy @ Hh & Get Started with RabbitMQ in Node,js In this article, we are going to connect two node.js applications with a queue using RabbitMQ. eSharmi Image by Author RabbitMQ is an open-source message broker software. Message Broker is the intermediary went hotween nrowider and client(s) that makes sure the . ’ on message is not lost. S22 Q| | Some alternatives: Apache Kafka, Amazon Kinesis etc. RabbitMQ implements AMQP (Advanced Message Queuing Protocol). AMQP is a protocol that helps in communication between services using messages. @SharmilaS RabbitMQ Installation If RabbitMQ is not installed in your system, install it from here based on your platform or you can use this docker image. Iwill be using the RabbitMQ docker image to run the RabbitMQ server locally. If you are familiar with docker, you can run the following command to run the RabbitMQ server in your system. docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.9-management Our server is running at port 5672 and it is ready to connect to services.RabbitMQ running This article guides you step-by-step with code snippets. For the full code, visit the Github repository link at the end of this article. Creating Node app 1 (Provider) Let's create a node.js app that will act as the provider. The role of the provider here is to send a message to the queue from where it reaches the client. (To get started with node,js, click here) Steps: 1. Create a node.js app using express. 2. Connect to the queue using the amqptib module. 3. Define an API to send a message to the queue(for testing). 4. Start with a simple Node app Let's create a simple node application and make it listen to port 4001 . * Create a new project folder and initialise npm in it. § mkdir provider-app $ cd provider-app$ npm init * Install the required modules: $ npm i express amqplib express -> to create a node,js application. amgplib -> to create a message broker. * Create index,js file and add the following, const express = require("express"); const app = express(); const PORT = process.env.PORT || 4001; app.use(express.json()); app.get("/send-msg", (req, res) => { res.send("Hello world") Ys app. listen(PORT, () => console. log("'Server running at port " + PORT)); Notice that we define a route /send-msg here, which we will be using later to senda message to the queue upon an API request. 2. Connection to the queue + Import amqplib module in the index.js file. const amgp = require("amqplib") ; Let's wrap our connection code in an async function since we need to work with promises in the code.(if you are unfamiliar with async/await and other js concepts, click here to get a quick overview of some of the must-know javascript concepts) var channel, connection; //global variables asyne function connectQueue() { try { connection = await amgp.connect ("amgp: //localhost:5672") ; channel = await connection. createChannel () await channel.assertQueue("test-queue") } catch (error) { console. log(error) + Let's take a look at the code that connects to the RabbitMQ server. connection = await amqp.connect(“amap://localhost:5672”) channel = await connection.createChannel() © Using the conect() method, we make a connection to the server which is running at port 5672. * We create a channel from the connection using which we can access the queues. (To understand more about connections and channels, visit this link.) | await channel.assertQueue(“test-queue”) * Inthe above line, we check for a queue named ‘test-queue’. If that queue does not exist, a new queue is created with the provided name (here, ‘test-queue’). Then, call the connectQueue function to initiate a connection when our app starts. connectQueue()* Let's define another async function, which can be used to send a message to the queue. async function sendData (data) { // send data to queue await channel. sendToQueue("test-queue", Buffer .from(JSON.stringify (data) )); // close the channel and connection await channel.close(); await connection.close() 5 + We pass the message to this sendData function using function parameters. ¢ The channel.sendToQueue() method is used to send the message to the specified queue. It takes two parameters, the name of the queue and the message to be sent. 3, Define an API for sending a message to the queue (for testing). « Let's modify the route ‘ /send-msg’ to send a message to the queue when it is called. We use the function sendpata() (which we defined earlier) to pass the message. app.get("/send-msg", (req, res) => { // data to be sent const data = { title : "Six of Crows", author : "Leigh Burdugo" + sendData(data); // pass the data to the function we defined console. log("A message is sent to queue") res.send("Message Sent"); //response to the API request » Creating Node app 2 (Client)Now, we create another node,js app that connects to the queue and acknowledges the message from the queue. Firstly, a simple express app $ mkdir client-app $ cd client-app $ npm init Create index.js file and add the following, const express = require("express"); const app = express(); const PORT = process.env.PORT || 4002; app.use(express.json()); app.listen(PORT, () => console. log("'Server running at port " + PORT)); The client-app runs on port 4002 . Connection to the ‘test-queue’ We make a connection to the test-queue in the same way as we did in the ‘provider- app’ Then, we consume the data from the queue and acknowledge it. const amgp = require("amqplib") ; var channel, connection; connectQueue() // call the connect function async function connectQueue() { try { connection = await amgp.connect ("amgp: //localhost:5672") ; channel = await connection. createChannel() await channel.assertQueue("test-queue") channel. consume("test-queue", data => {console. log(*${Buffer. from(data.content) }*) channel.ack (data) ; +) } catch (error) { console. log(error) ; + The message from the queue is read using the consume() method. It takes the name of the queue ( here, ‘test-queue’ ) as a parameter and returns the message from the queue as a callback. channel..consume(“test-queue", data => { console. log(*${Buffer. from(data. content)}*); channel. ack (data) ; HW The channet.ack() function is used to acknowledge that the particular message has been received by the ‘client-app’. Testing Let’s try to send a message from the provider service to the client service. * Go to the terminal, run the provider app using, $ cd provider-app $ node index. js* Run the client application in another terminal. $ cd client-app $ node index. js Now, let’s try to send a message from the provider app. Our provider app is running at http:/localhost:4001. When the route ‘/send-msg’ is called, the message will be sent to the queue. Since it’s a GET request, I’m just going to enter the URL in the browser, you can also use tools like Postman to send the request.C QQ @ localhost Message Sent Request to /send-msg Let's look at the console now. * provider-app Provider sending message to the queue * client-app eived Client displaying the message from the queueThe message has been sent from one service to another, using a queue. We now implemented a simple queueing service using RabbitMQ in nodejs. GitHub link — https://github.com/SharmilaS22/medium-rabbitmq-nodejs Hope this article helped to get started with RabbitMQ! Happy learning! Ifyou liked my article, feel free to buy me a coffee here. 7 Sa https:/www.buymeacoffee.com/sharmilas Technology JavaScript. +=“ DevOps.-——(Nodejs. Enjoy the read? Reward the writer." Your tip will go to Sharmila S through a third-party platform of their choice, letting them know you appreciate their story.Get an email whenever Sharmila S publishes. Emails villbe sent to manescue2I0@gmallcom, Not you? (Ss Subscribe \ )
You might also like
Mastering RabbitMQ - Sample Chapter
PDF
No ratings yet
Mastering RabbitMQ - Sample Chapter
24 pages
Getting Started With RabbitMQ and CloudAMQP PDF
PDF
100% (1)
Getting Started With RabbitMQ and CloudAMQP PDF
68 pages
High Powered Messaging With: James Carr Software Engineer OCI
PDF
No ratings yet
High Powered Messaging With: James Carr Software Engineer OCI
37 pages
Getting Started With RabbitMQ and CloudAMQP
PDF
No ratings yet
Getting Started With RabbitMQ and CloudAMQP
52 pages
RabbitMQ - Microservices - NestJS - A Progressive Node - Js Framework
PDF
No ratings yet
RabbitMQ - Microservices - NestJS - A Progressive Node - Js Framework
10 pages
RabbitMQ With C# - Code-Adda - Cara Instalasi
PDF
No ratings yet
RabbitMQ With C# - Code-Adda - Cara Instalasi
12 pages
RabbitMQ A Comprehensive Guide
PDF
No ratings yet
RabbitMQ A Comprehensive Guide
3 pages
Chapitres 2 Fondements Des Systèmes Répartis VC
PDF
No ratings yet
Chapitres 2 Fondements Des Systèmes Répartis VC
82 pages
Messaging With Rabbitmq in Golang
PDF
No ratings yet
Messaging With Rabbitmq in Golang
33 pages
TP1 RabbitMQ v3
PDF
No ratings yet
TP1 RabbitMQ v3
6 pages
Clojure and Rabbit MQ
PDF
No ratings yet
Clojure and Rabbit MQ
7 pages
Message Broker With RabbitMQ - From Microservices To Server Instances
PDF
No ratings yet
Message Broker With RabbitMQ - From Microservices To Server Instances
5 pages
Rabbit MQ
PDF
No ratings yet
Rabbit MQ
7 pages
Mazon (Rabbit) MQ
PDF
No ratings yet
Mazon (Rabbit) MQ
9 pages
Programming Amazon SQS and SNS Using The AWS Nodejs SDK V1.04
PDF
No ratings yet
Programming Amazon SQS and SNS Using The AWS Nodejs SDK V1.04
26 pages
Chapitre 2 Fond SYS Répartis
PDF
No ratings yet
Chapitre 2 Fond SYS Répartis
63 pages
Rabbitmq
PDF
No ratings yet
Rabbitmq
24 pages
AMQP
PDF
No ratings yet
AMQP
6 pages
RabbitMQ Tutorial - Hello World! - RabbitMQ
PDF
No ratings yet
RabbitMQ Tutorial - Hello World! - RabbitMQ
8 pages
Part 3the RabbitMQ Management Interface - CloudAMQP
PDF
No ratings yet
Part 3the RabbitMQ Management Interface - CloudAMQP
17 pages
RabbitMQ Checklist For Production Environments - A Complete Guide - CloudAMQP
PDF
No ratings yet
RabbitMQ Checklist For Production Environments - A Complete Guide - CloudAMQP
6 pages
Message Queue & Pub-Sub
PDF
No ratings yet
Message Queue & Pub-Sub
4 pages
Sisteme Distribuite Documentatie Laboratorul 2
PDF
No ratings yet
Sisteme Distribuite Documentatie Laboratorul 2
13 pages
DC 9225 Exp4
PDF
No ratings yet
DC 9225 Exp4
9 pages
Scaling Web Apps With Rabbitmq: Álvaro Videla - The Netcircle
PDF
No ratings yet
Scaling Web Apps With Rabbitmq: Álvaro Videla - The Netcircle
117 pages
CS10 Communication
PDF
No ratings yet
CS10 Communication
22 pages
JP Unix
PDF
No ratings yet
JP Unix
19 pages
Week 6
PDF
No ratings yet
Week 6
7 pages
11 2 4-Amqp
PDF
No ratings yet
11 2 4-Amqp
14 pages
Eventmachine Works With Amqp
PDF
No ratings yet
Eventmachine Works With Amqp
24 pages
Spring RabbitMQ For High Load
PDF
No ratings yet
Spring RabbitMQ For High Load
50 pages
Rabbit MQ y Symfony2
PDF
No ratings yet
Rabbit MQ y Symfony2
81 pages
RAbbitMQ With Python
PDF
No ratings yet
RAbbitMQ With Python
4 pages
Plone, Rabbitmq and Messaging That Just Works: Asko Soukka & Jukka Ojaniemi
PDF
No ratings yet
Plone, Rabbitmq and Messaging That Just Works: Asko Soukka & Jukka Ojaniemi
28 pages
AMQP
PDF
No ratings yet
AMQP
19 pages
RabbitMQ Urbiotica
PDF
No ratings yet
RabbitMQ Urbiotica
5 pages
Active MQ
PDF
No ratings yet
Active MQ
18 pages
Getting Started With RabbitMQ and CloudAMQP
PDF
No ratings yet
Getting Started With RabbitMQ and CloudAMQP
94 pages
Getting Started With RabbitMQ and CloudAMQP
PDF
No ratings yet
Getting Started With RabbitMQ and CloudAMQP
94 pages
Interservice Communication
PDF
No ratings yet
Interservice Communication
9 pages
Programming Percy
PDF
No ratings yet
Programming Percy
65 pages
ESB Usage Guide
PDF
No ratings yet
ESB Usage Guide
23 pages
Rabbit MQ
PDF
No ratings yet
Rabbit MQ
151 pages
Enterprise Messaging With Apache Activemq: James Strachan
PDF
No ratings yet
Enterprise Messaging With Apache Activemq: James Strachan
46 pages
Choosing Your Messaging Protocol AMQP, MQTT, or STOMP
PDF
No ratings yet
Choosing Your Messaging Protocol AMQP, MQTT, or STOMP
3 pages
MQTT - Iot Messaging Protocol Francisco Quintero Lead Firmware Engineer
PDF
No ratings yet
MQTT - Iot Messaging Protocol Francisco Quintero Lead Firmware Engineer
33 pages
FMQC RTL Guide
PDF
No ratings yet
FMQC RTL Guide
297 pages
JBoss AMQ 7 Deep Dive
PDF
No ratings yet
JBoss AMQ 7 Deep Dive
35 pages
Node Red
PDF
No ratings yet
Node Red
14 pages
2
PDF
No ratings yet
2
8 pages
IoT Unit 4
PDF
No ratings yet
IoT Unit 4
5 pages
Getting Started With MQTT My Electronics Lab
PDF
No ratings yet
Getting Started With MQTT My Electronics Lab
24 pages
Messaging With RabbitMQ - Logical Link Diagram
PDF
100% (1)
Messaging With RabbitMQ - Logical Link Diagram
11 pages
Go Programming Language Tutorial (Part 6)
PDF
No ratings yet
Go Programming Language Tutorial (Part 6)
7 pages
Self Study Topics 1
PDF
No ratings yet
Self Study Topics 1
14 pages
Getting Started With RabbitMQ and CloudAMQP
PDF
No ratings yet
Getting Started With RabbitMQ and CloudAMQP
162 pages