Beginners guide to RabbitMQ - (Backend Development Series)
Updated: Dec 9, 2022
Open source multiprotocol Distributed Message Queue
This is the most complete tutorial on rabbitmq with node.js for beginners on the internet.
The best part?
Everything in this tutorial works flawlessly without any errors.
If you are someone who wants to exploreRabbitMQ or message queues in general or are starting to work on RabbitMQ or researching RabbitMQ as a message queue service, this blog is for you. This guide will give you an understanding of what is a rabbitmq message queue, how the publisher-consumer model would work for rabbitmq, what are the steps to set up a rabbitmq connection, and also set up the publisher/consumer connections using nodejs. By the end of this guide, you will be able to create a basic task manager, where the publisher will publish messages and the desired consumer will receive the messages.
RabbitMQ has over 10.1K GitHub stars and 3.7K GitHub forks. Here’s a link to RabbitMQ's open-source repository on GitHub - https://github.com/rabbitmq/rabbitmq-server
Let's dive right into the RabbitMQ tutorial -
1. What is RabbitMQ? What problem does it solve?
RabbitMQ is an open-source distributed message queue written in Erlang. It supports many communication protocols. In this blog, I will be using the AMQP (Advanced Message Queuing Protocol) - it's a set of standards to be followed while communicating.
Note: RabbitMQ is also called a message broker service or message-oriented middleware.
When to use RabbitMQ?
RabbitMQ was introduced to solve the problem of Spaghetti Mesh Architecture. In this architecture, every client talks to every other client to get the work done. The problem with this is any change made in any one of the clients would impact all the other clients it is connected to, which is obviously not optimal. A middleman/middle layer is required to handle these connections. This is where a message broker like rabbitmq comes in.
2. Pieces of the RabbitMQ Architecture
So how does RabbitMQ work? The RabbitMQ architecture basically comprises 6 pieces. These are
Rabbitmq server - The middleman/middle layer that we talked about above is actually the RabbitMQ server. It always listens to messages ( on port 5672 by default). It has to listen always as it is using TCP.
Publisher - This is basically a client that wants to send some information/message to another client. The publisher establishes a stateful 2 way TCP connection between itself and the RabbitMQ server. The publisher basically tells the rabbitmq server - "Hey, I want to send this message "foobar" to client number 2". It uses the Advanced Message Queue Protocol (AMQP) protocol for this, (AMQP is basically a variant of the TCP protocol).
Consumer - The consumer tells the server - "The line is open for sending messages." the RabbitMQ server would then send messages to the client as and when new messages are received by the server from the publishers. It also establishes a stateful 2 way TCP connection between itself and the RabbitMQ server and it also uses the Advanced Message Queue Protocol (AMQP) protocol. ( Note: If you want to know more about the publisher-subscriber model, check out this article - https://www.thegeekyminds.com/post/publisher-subscriber-model-system-design-architecture )
Channel - A channel is basically a smaller version of the connection. One connection can have multiple channels. The publisher can send messages on specific channels and the consumer can choose to listen on one or multiple channels. The goal here is that not every message the publisher sends needs to be sent to all the consumers. For example - A publisher sends out a message on a channel named "football_score". There can be many consumers connected to the rabbitmq server but the consumers who are listening to the channel named "football_score" will receive the message. It is basically an application of multiplexing. ( Note: