Codementor Events

Building a Simple Messaging System Using ActiveMQ

Published Feb 05, 2021Last updated Aug 03, 2021
Building a Simple Messaging System Using ActiveMQ

So, you have decided that you need to use a message broker in your application, but you need a little push to figure out how you can actually integrate it? If so, then continue reading this article and by the end you will know exactly how to do so.

Although there are many message brokers out there, we will focus on ActiveMQ in this article.
ActiveMQ is a great open-source messaging service, built with Java that can be safely integrated into any architecture. It supports most protocols and is perfect for both small and heavy loaded applications as well.

As mentioned, ActiveMQ supports most protocols and can be used with all programming languages that use any of these protocols. Throughout this article, we will use NodeJS as a programming language, STOMP as a communication protocol, and Windows as the operating system. If you use something else, then don’t worry, because changing one of these languages with something else is simple as the procedure remains the same.

The Code 👨‍💻

Step 1: Prerequisites

ActiveMQ is an implementation of JMS, therefore to make use of it we need to install Java Runtime Environment (JRE). Download the JRE for your operating system and follow the instructions from the install wizard.

Once you’ve installed this tool, check if the JAVA_HOME environment variable was set correctly to the directory where the JRE is installed.

To check this, simple press WINDOWS-KEY + R and type %JAVA_HOME%. If you press enter and the file explorer opens to the installation directory, then you are good to go.

If an error appears instead, it means that you need to manually set this environment variable. For this, check this short article.

Step 2: Install ActiveMQ

To install ActiveMQ, firstly go and download the latest and most stable version from the official website and extract the ZIP files to a directory of your choice. The directory where you will extract the files will be like an "installation directory", so go ahead and extract it somewhere where you know you won't accidentally delete it.

Once the files are extracted, open a command prompt in that directory and run the following command:

RAW

To verify that everythink works as expected, navigate to http://127.0.0.1:8161/admin/ and use admin as credentials for both username and password.

The ActiveMQ console should open as such:

Step 3: Node JS setup

For Node JS, we will create 2 applications, a consumer and a producer, to demonstrate how to create a simple messaging system.

Therefore, in a new folder, open a command prompt and run the following commands:

RAW

The first command will create a new package.json file and the second one will install the package stomp-client that we will use to communicate using STOMP protocol.

Step 3 - Connect to ActiveMQ using STOMP

STOMP is enabled by default in ActiveMQ and has the following definition:

RAW

Therefore, to connect to ActiveMQ using the STOMP protocol, we need to connect to localhost or to the IP of the machine on which ActiveMQ is running at port 61613.

Translated in NodeJS language using stomp-client package, it looks like the following:

RAW

This code will be present in both applications.

Step 4: The Consumer

The first application will play the role of a consumer. This application will subscribe to a certain topic and will listen to all the messages that are sent through.

Create a file named consumer.js and paste the following code.

RAW

In this application, we subscribed to a queue named notifications and we will receive all the messages that are sent in the parameter named body along with its headers in the parameter named headers.

Let’s start this application using the following command node consumer.js.

Once the application is running, you will see this new queue in the ActiveMQ web console under Queues tab.

N``umber of consumers is set to 1, because we only have 1 application that has subscribed to this topic.

Step 5: The Producer

Next, create a new file named producer.js with the following code:

RAW

Here, instead of subscribing to a queue, we will send a message. Once the message has been sent, we will disconnect.

The message that we send must be a string, therefore we want to send an object we will need to convert to a string.

This can be easily achieved using JSON.stringify() and then JSON.parse() to convert the received string back to an object.

If we start this application using the same command node producer.js we should see the following output in the consumer’s terminal.

This update will also reflect in the ActiveMQ console:

Now we have 1 consumer, 1 message enqueued and 1 message dequeued.
In other words, 1 message was sent and 1 message was received.

Wrap up

This article should give you a start in building web applications with NodeJS that integrates ActiveMQ as a message broker using the STOMP protocol on Windows 10.
However, if you are on a different OS, then check out this article from ActiveMQ which will show you how to install it on your desired system.

Regarding the platform, the code will be the same. If you want to use another protocol instead of STOMP, check what other libraries are out there that already have the functionality written for your protocol and programming language of interest.

Cover Photo by Andrej Lišakov on Unsplash

Discover and read more posts from Anton Lawrence
get started