🐰RabbitMQ 🐳Docker 📮NPM 🧩Node.JS
Used in this project:
- RabbitMQ - open-source, distributed messaging queue
- Docker - software platform to build, test, and deploy containerised workloads
- NPM - default package manager for the JavaScript
- Node.js - open-source, cross-platform, back-end JavaScript runtime environment
- with docker installed, run:
docker run --name rabbitmq -p 5672:5672 rabbitmq
- open repo in vscode
- in CLI, run:
npm run publish "your message here"
npm run consume
tutorial --> https://www.youtube.com/watch?v=Cie5v59mrTg
RabbitMQ - open source, distributed messaging que
Written in ERLANG language, best suited for massively scalable soft real-time systems with high reliability
Supports many communication protocols
RabbitMQ Is a soft real-time system
It was originally trying to solve a problem called Spaghetti Mesh Architecture
The left figure is clients communicating to each other, the right is a queue service such as RabbitMQ
https://i.ibb.co/xGjzsxN/Impact-N.png
Rabbits own protocol = AMQP - Advanced message queue protocol
RabbitMQ Components: index
- AMQP
- RabbitMQ Server
- Channels
- Queue
- Publisher
- Consumer
- Exchange
- Protocols
https://i.ibb.co/GJwhpfY/RabbitMQ.png
RabbitMQ top level abstract view:
- The RabbitMQ server
- The Publisher
- The two way Stateful coms connection between the Publisher and RabbitMQ using AMQP
- The Consumer
- Another two way Stateful coms connection between the Consumer and RabbitMQ using AMQP
- Channels
- Exchanges
RabbitMQ Server
-
Rabbit uses a middle/Central layer that all clients communicate to
-
This solves the spaghetti mess
-
Could be multiple servers, could be distributed
-
Uses listens to port 5672 by default
-
It is using TCP so it has to listen
-
Pushes message from publishers to intended consumers
Publisher
-
“Hey I am the client and I want to publish a message to the consumers that are interested in this message”
-
Establishes a stateful TCP connection between itself(the publisher) and the RabbitMQ server
-
It is a 2 way/bi directional communication connection, therefor the underlying transportation protocol is raw TCP.
-
Not HTTP, but rather raw TCP
-
AMQP wraps around the TCP. This adds extra stuff like its own HEADERS
-
Publishers and Consumers are not aware of Queues. However they are aware of Exchanges
-
Publishers can freely be created and killed, unlike consumers who upon creation must stay alive
Consumer
- Receives message from server
- Must ‘acknowledge’ messages in order to clear them from the queue
- Message acknowledgment is very complicated and gets extremely in-depth, because its so important
- RabbitMQ guarantees 1 successful acknowledgment
Channels
-
Channels take 1 stateful TCP bidirectional connection between Publisher/Consumer and RabbitMQ server, and, splits it up.
-
For example, Let’s say a particular view/page on your frontend has 5 consumers, well, rather than each consumer creating a connection to Rabbit server, the client can create One connection, and share it among each consumers. Each consumers gets their “Channel” in the connection to the Rabbit server
-
Channels keep things separated. each consumers connection is separated from other consumers
-
The technical terminology for Channels is Multiplexing
-
HTTP/2 uses multiplexing
Exchanges
- Exchanges take care of propagating messages into Queues
- The default exchange works for most cases
- Advanced uses. Kinda like Django middleware
- Support algorithms/filters to ‘fan out’ to different queues and stuff
AMQP
- Advanced Messaging Queue Protocol
- Basically wraps raw TCP with extra stuff like HEADERS
- Has ‘clients’,
- In js, using the require(“amqplib”) , If anything returns a Bluebird, then that is a promise so must await it
How to RabbitMQ
-
With docker installed
-
run: docker run hello-world
-
You should see Hello World! In terminal. This confirms docker is installed correctly
-
Now run: docker run --name rabbitmq -p 5672:5672 rabbitmq
-
docker run (a container) --name (immediately name your new container) rabbitmq -p (expose this port) port:port 5672:5672 rabbitmq(the actual rabbitmq container image that docker will pull from public container repository)
-
When the image is downloaded and Spinned(span) up, that terminal will be ‘occupied’ as the running process of the container
-
Create project folder rabbitmq
-
Open folder in vscode
-
Make publisher.js
-
Run in terminal: npm init -y (creates package.json. -y tell npm “I know what I am doing just create package.json”)
Tips:
What we are going to build: https://i.ibb.co/9G6PFWg/Async-Job-Manager.png
Asynchronous job execution manager/engine.
- Publisher will publish jobs
- The appropriate consumer for that job type will get the message and do the job
MIT