RSMQ- Message queueing with Node js and Redis

sparkidev
4 min readJun 27, 2019

--

So what is message queuing?

It’s simple if two processes need to speak to each other they should use a shared memory location to exchange data (OS-class 101 Interprocess communication 😄 ).

Consider the process as two different services. Service #1 may be a web backend and Service2# may be a system software that does a specific task that it's meant to do, to synchronize stuff’s between both services they need to access the same memory location, and obviously, the representation in memory will be a queue data structure.

Please visit the link if you don't know how a queue works: https://hackernoon.com/the-little-guide-of-queue-in-javascript-4f67e79260d9

For the purpose, we are using Redis as a shared memory queue.

Redis — https://redis.io/topics/quickstart

REDISMQ- https://www.npmjs.com/package/rsmq — — For a server to create a queue and send messages

REDISMQ-WORKER — https://www.npmjs.com/package/rsmq-worker — For Service workers to receive messages based on the queue

We have an application server where an email service is there to send the mail to a user, based on some events happening inside the server. Let us decouple the email service from the server and make them as a service worker 😉 .

The arch in detail :

Server 1 and 2 are Node JS servers with the same app running in different instances

Service workers will send mail based on the data that you send to the queue

The flow of data is unidirectional ( Server — → Service workers)

Once the message is read its popped out from the queue

Redis has done some semaphore mechanisms to avoid deadlocks

Let’s hope that there is a node server app running on server 1 and server 2

#install the package  on to your existing node application npm install rsmq --save 
touch emailque.js#just creat a file in a directory that's more suitable for you

open emailque.js

I have created a variable called email driver which holds the Redis connection properties, so what’s the purpose of ns there, its the namespace prefix used for all keys created by RSMQ . We export the methods as objects to be more suitable for imports.

create_queue method is to create a specifically labeled queue called email

send_email method is used to send the mail-data to the queue (eg sender address content etc…..)

You may catch an error if a queue is already created and don’t worry about that we have used a try-catch statement to get a look into that

we just used some async/await to make it tidier rather than using a promise chain

Ok, so many of you guys write the server code in an index.js file so just import it and initiate the create() method, just to make sure the email queue is present in the Redis instance on startup

require('./path_to /emailqueue').create()

suppose we have got the user data from a specific route and assume that we just passed that data as to an object called user_data which includes there email address, content to send, etc.

JSON.stringify is used to make the message a string type. so it's more suitable

😉 it’s done, start the server and send some data to the queue

let’s create the code for the Service worker, initiate and install the package as given below

#Lets get a fresh install here
mkdir service_worker && cd service_worker/
npm init -y
npm install rsmq-worker --save
touch emailworker.js
touch mail.js
/*copy the module that you used to send mail and
export it as a function
*/

The async model is missing on the rsmq-worker module but a call back could do the job!!!! 😜

Start the emailworker.js and see the magic that happens 👍

Do something different, there are a lot more options in both the worker and rsmq modules dig through that and decouple some services on your existing application.

--

--