Where Do Message Brokers Fit in My Stack?

We don’t talk about middleware much, but we should. I get the appeal of modern frontend frameworks like Next JS. A great frontend framework combined with skillful web design can provide a great user experience, but if the user experience doesn’t match the service experience, you may not be much better off.

When you make that purchase or place that order, there are things that happen behind the scenes we often take for granted. That’s often where your middleware comes in. Under the middleware umbrella are what we call message brokers, and one of the popular open-source message brokers that has been getting a lot of attention lately is RabbitMQ. Those who do Magento Web Development may have noticed the option of connecting RabbitMQ to Magento.

Getting Started with RabbitMQ

It’s fairly easy to get started with it. You can get a free managed RabbitMQ server hosted on AWS at:


Otherwise, it can be installed on a variety of operating systems including Ubuntu– sometimes by executing one command. If you want to install RabbitMQ on Microsoft Windows, you can do so with an admin user using the official installer or Chocolatey. I prefer Chocolatey, since it will install Erlang if you don’t have it installed already.

There are more ways to deploy RabbitMQ than I can reasonably mention in this article. This includes VMWare’s Tanzu, Terraform, and Docker Compose.

RabbitMQ integrates well with a number of the common technologies you may be already using. For example, if you’re using the ElasticSearch-LogStash-Kibana (ELK) stack, you can use the RabbitMQ input plugin for Logstash to pull events from a RabbitMQ queue. If you’re looking for RabbitMQ monitoring, both New Relic and Datadog are popular options. 

And then there’s the flexibility. You can develop cross-language messaging with a wide variety of programming languages. This includes scripting languages like JavaScript, PHP, and Python. To develop a client application to communicate with RabbitMQ, just download the Application Programming Interface for your programming language.

An Example Using NestJS

As an example, let’s use NestJS– a framework branded as being useful for developing scalable and efficient server-side applications in Node.js. We’ll be doing some server-side coding. You may notice JavaScript is not only used for front-end development which we generally associate with HTML, CSS, and JavaScript used together, but it can also be used for backend development as well.

Scaffold a NestJS Project

Once you have Node.js and npm/yarn installed, it’s fairly easy to get started with the framework. Let’s use NestJS to scaffold a project with the Nest CLI. Install the Nest CLI globally and scaffold a new project.

$ npm i -g @nestjs/cli $ nest new nestjs-rabbitmq-project
Code language: Bash (bash)

You’ll get the opportunity to choose whether you want to use the npm or yarn package manager. When the process is complete, you can cd into the directory, and assuming you’re using yarn, can execute:

$ yarn run start
Code language: Bash (bash)

To verify everything is working properly, you can open a web browser tab and navigate to http://localhost:3000 to see the ‘Hello World!’ message. You can stop it using ctrl-c. 

Given RabbitMQ is an AMQP server we’ll need to install some AMQP client libraries to develop a client. Add the following to your project:

$ yarn add @nestjs/microservices amqplib amqp-connection-manager
Code language: Bash (bash)

If you’re planning on using a .env file, you can also add the following:

$ yarn add @nestjs/config
Code language: Bash (bash)

Now, you should be ready to open the project with your IDE, so we can create an example standalone application. We’ll start with the main.ts file and make other changes where needed.

 Upon first glance, it may feel like an Angular application. NestJS does have some Angular dependencies.

 A Little Server Side Coding

If you open the main.ts file, you should see the following:


import { NestFactory } from '@nestjs/core'; import { AppModule } from '.app/app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } bootstrap();
Code language: TypeScript (typescript)

We’re going to make some modifications in order to create a client application for our RabbitMQ server. Let’s start out with a bare bones NestJS microservice:


import { NestFactory } from '@nestjs/core'; import { Transport, MicroserviceOptions } from '@nestjs/microservices'; import { AppModule } from '.app/app.module'; async function bootstrap() { const app = await NestFactory.createMicroservice<MicroserviceOptions>( AppModule, { transport: Transport.TCP, }, ); app.listen(() => console.log(‘Microservice is listening’)); } bootstrap();
Code language: TypeScript (typescript)

You should be able to execute yarn run start in the terminal and, after the app starts up, it should output: ‘Microservice is listening.’

At this point, we can configure this app to listen to our RabbitMQ server. Let’s start with the main.ts file:


import { NestFactory } from '@nestjs/core'; import { Transport, MicroserviceOptions } from '@nestjs/microservices'; import { AppModule } from '.app/app.module'; async function bootstrap() { const RMQ_URL = <YOUR_RABBITMQ_URL>; const RMQ_QUEUE_NAME = <YOUR_QUEUE_NAME>; const app = await NestFactory.createMicroservice<MicroserviceOptions>( AppModule, { transport: Transport.RMQ, options: { urls: [RMQ_URL], queue: RMQ_QUEUE_NAME, queueOptions: { durable: true, }, }, }, ); app.listen(() => console.log(‘Microservice is listening’)); } bootstrap();
Code language: TypeScript (typescript)

You may notice your RabbitMQ server credentials can be a part of the URL. Now, we’ll have to make some changes to the AppController class so the app knows how to handle the RabbitMQ messages when they come:


import { Controller } from '@nestjs/common'; import { EventPattern } from '@nestjs/microservices'; @Controller() export class AppController { @EventPattern() async handleNotifications(data: any) { console.log(typeof data, 'message received', data); } }
Code language: TypeScript (typescript)

You may notice we didn’t say anything about the app.service.ts file. We won’t need that for this example. Its reference can be safely removed from the app.module.ts file.

At this point, you should be able to execute yarn run start to get it running. You can now publish a message to your queue. 

The general structure has to be {“data”: …}. You can start with something simple like {“data”: “Hello world!”}. The result in the terminal should be simply:

... Hello world!

All done. I think you can take it from here.


If I’ve sold you on RabbitMQ at all, know there is a RabbitMQ Summit 2021 planned for July, but it will be virtual. Currently, there is a waiting list for those interested in attending. Just submit the form with your basic information and they should eventually get back to you.

You May Also Be Interested In


Messaging that just works. https://www.rabbitmq.com/#features. Accessed 3/27/2021.

Part 1: RabbitMQ for Beginners – What is RabbitMQ? https://www.cloudamqp.com/blog/part1-rabbitmq-for-beginners-what-is-rabbitmq.html. Accessed 3/27/2021.

Chocolatey Software | RabbitMQ 3.8.11. https://chocolatey.org/packages/rabbitmq. Accessed 3/27/2021

RabbitMQ | Magento 2 Developer Documentation. https://devdocs.magento.com/guides/v2.4/install-gde/prereq/install-rabbitmq.html. Accessed 3/28/2021.

Rabbitmq input plugin | Logstash Reference [7.12]. https://www.elastic.co/guide/en/logstash/current/plugins-inputs-rabbitmq.html. Elastic. Accessed 4/15/2021. 


Comments are closed.