Incorporating the RabbitMQ tutorial into our own project

TekLoon
TekLoon
Jul 29 · 4 min read
Photo by Jacky Chiu on Unsplash

Recently, when I tried to go through the RabbitMQ tutorials, I found myself struck by a wave of boredom and sleepiness. They were tough to get through!

Hence, I thought I would create something interesting with RabbitMQ instead of just following the tutorial. Turns out it works so well that my adrenaline gets flowing and it keeps me awake till after midnight.


Project Background

I love motivational quotes. Thus, I want to scrape motivational quotes every minute. This led me to create two microservices. Each microservice operates independently of each other and only performs its own respective task. This is how we achieve separation of concern.

  • TimerService
    - Publishes a message every minute to RabbitMQ server
  • QuoteService
    - Scrapes quotes from the Internet when it receives the event from TimerService

Now that we have completed the solution design. Let’s get our hands dirty and start building.


Prerequisites

Before we start to develop the microservices, several tools and frameworks are required:


Step-by-Step Guide

Here is TekLoon’s Dev Rule No 1:

The easy entry will allow you to complete the first task and gain the confidence to face the upcoming challenges.

Step 1: TimerService development

Let’s start by creating our TimerService:

Code for TimerService. View full code here

Let’s look at what we did here. We:

  • Created a connection to our RabbitMQ server.
  • Created a channel
  • Created a direct, non-durable ‘quote’ exchange
  • Published the message to the exchange during each interval (60 seconds)

This code fulfills the purpose of TimerService.

Step 2: QuoteService development

Let’s continue with our QuoteService development. The main function of this service is able to scrape a motivational quote from the Internet when it receives a message from the RabbitMQ queue.

First, create a consumer and listen for the event sent by TimerService. Let’s make it very simple; when we receive the message we call the scrapQuoteOfTheDay() function:

Next, we proceed to scrape the quote from the Internet:

I’m getting my quotes from wisdomquotes.coms and writing them to a JSON file. Pretty straight forward right?

Now that we have our business logic in place, let’s make an HTML page to display the quotes that have been scrapped. After doing some research, the simplest way to dynamically render the HTML in ExpressJS is by using the template engine Pug. Let me show you how I did it:

This is the UI that will be created based on the index.pug template.

You can get the full source code for QuoteService here.

Step 3: Express Server settings for QuoteService

In order for QuoteService able to listen to RabbitMQ queue, we would have to do some setup during our server initialization. Besides, there is also rendering the quotes HTML based on the index.pug template we have created in step two.

Aside from booting up the web server, this configuration also fulfills the following purpose:

  • Renders the index.pug template (line 17)
  • Listens to RabbitMQ queue (line 23)

Step 4: Run and test It !!!

Let’s run our project and test it locally. Ultimately, you can get my full source code from Github.

  1. Let’s boot our QuoteService component. Go to QuoteService folder and do npm run start
  2. Boot up our TimerService component by going to the TimerService folder and running npm run start
  3. Below are screenshots for you if you run it successfully:
TimerService publishes two events to the RabbitMQ exchange
QuoteService is able to consume two events listening to the RabbitMQ queue
QuoteTimer HTML page rendering able to render 2 quotes that scrape from WisdomQuotes

Conclusion

RabbitMQ is definitely very easy to learn and has a clear tutorial on their own website, however, tutorials are way more fun when you incorporate into your hobbies or interests.

I personally like RabbitMQ a lot and would love to use it to build event-driven microservices for upcoming projects.

Finally, there are a lot of ideas that flashed into my mind while I was developing this, such as:

Thanks for reading and feel free to give feedback.

References

Here are the references that I used while building this project:

Better Programming

Advice for programmers.

TekLoon

Written by

TekLoon

Javascript Developer. I explain via code and words. Personal Mantra: Life is about trying stuff, validating stuff and retry some stuff if it is not working

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade