Messaging services with nodejs Part 2

Well, finally we made the second part of Messaging services with nodejs, that you have been waiting for. 

For this part, we will take a look con request / replay pattern implemented with zmq (Zero Messaging Queue). If you did´t read the first part of this post, I recommend you to take a look here 🙂

The request / replay is a very common pattern. The pair request / replay communicate in lockstep, a request comes in, then a replay goes out, incoming requests are queued and then dispatched by zmq.

The Replay

So, let’s start coding, first we will code the replay service. The idea on this example is to show you the way that zmq implements the pattern. The replier will receive a request for a file, then replay with the content than the requester ask.

Create a new file, name it viking-reply.js  and copy the following code into it:

'use strict';
const
  fs=require('fs'), 
  zmq=require('zmq'),
  vik_responder=zmq.socket('rep');

vik_responder.on('message',function(data){
  letrequestData=JSON.parse(data);
  fs.readFile(requestData.file,function(err,content){
     let response= {
         fileMessage:content.toString(),
         timestamp:Date.now()
     };
     vik_responder.send(JSON.stringify(response));
  })
})

vik_responder.bind('tcp://*:3000',function(err){
  console.log('waiting for some viking requester :)');
})

What is this code going to do for you? Well, the first lines import the fs module (to read and write the files) and the zmq module (the library that we imported earlier). After that, we create the responder vik_responder.

The vik_responder.on waits for a “message” event to happen, parse the request from the raw data and read it with fs.read operation from the file system, the file named ass reaquestData.file. When fs.read   finishes (asynchronously), the code invokes the method vik_responder.send  to send the requester to the response that itis waiting for.

The vik_responder binds to port 3000 with vik_responder.bind. So, if we run on the  viking_reply.js on the node console, the responder will be ready :)!

viking_requester

The Requester

Let’s code the requester!

In order to do this you need to create a file, name it viking_requester.js and copy the following code into it:

'use strict';
const
   zmq=require('zmq'),
   filename=process.argv[2],
   vik_requester=zmq.socket('req');

vik_requester.on('message',function(data){
   let responseData=JSON.parse(data);
   console.log("The response from the viking reply is: "+responseData.fileMessage);
});

vik_requester.connect("tcp://127.0.0.1:3000");

let request = {
   file:filename
}

vik_requester.send(JSON.stringify(request));

The requested code is very simple, the first line imports the zmq library. It  gets (from the arguments) the file name that we want to request into filename.  You also need to create a requester named vik_requester.

After this, we listen to incoming message events and parse the raw data into responseData. Finally, to make the request into the responser, we  need to do  the vik_requester.send (of corse we need to make a JSON with a specific format for the responder ).

viking_requester

Once the previous steps are done we will have the requester endpoint of the pair request / replay pattern!

If you want to download this code, please follow this link

One  issue that we have with this pair is that the application operates only on one request or one response at a time, so we don’t have parallelism, even when the response program is reading the file, node’s event loops still spin while the fs.readFile is processing. Therefore this pair doest not have a high performance.  Stay tunned because on part 3  we will see the way that we could create a cluster to increase this performance.

Thanks for reading us!

 

 

Related posts