Sunday, May 28, 2017

Zero MQ : implementation of Publisher/Subscriber model using node

Zero MQ is a very easy technology to implement various message passing technology. Here I am trying to implement one of most common pattern Publisher/Subscriber model  with  Node JS and Zero MQ .

In Publisher/Subscriber model , Publisher (server) publish a message , and number of client that can subscribe it to it.

Before starting to code , Plz go ahead and intall zmq in your machine . You can follow step by step command in the below github gist
https://gist.github.com/cdjhlee/b8e3c927a01b0948b42d

Next you need to install zmq node module in your machine. I have installed it "globally" .

Let now , begin our Publisher. Here in the below code our Publisher is constantly watching a file and send a message when it find that file has been change. Let assume the file name is "pub.txt".

Here I have added the fs.watch function that monitor the change in the above file and send message to all the connected client when there is change in file.So when ever u want to trigger a message all u need to make change in pub.txt file.

I have written 2 subscriber one that subscribe to cat and other to dog. Now when the server , send the message . The respective client capture the message and print it to console.





Thursday, May 25, 2017

Node JS : understanding the event loop in Javascript.

Most of the time , JavaScript developer have the confusion around the concept of event loop. How event loop , libuv and Google V8 interpreter works  together that make JavaScript I/O non-blocking and asynchronous.

Most of us will one-way or other , has/have encountered the question in the interview or have been asked to explain the event loop or asynchronous concept in small JavaScript script with "setTimeout" function with a callback.In many ways , the concept of event loop is a kind of  hard shell to crack for people coming from different programming background.

Recently I came across , Philip's lecture were he has not only explained the flow of code from stack , to callback queue to event loop  successfully but also demonstrated it very well.
He has even develop small tool to give demo of event loop flow and how various component of Javascript interact with each other . http://latentflip.com/



Hope the above lecture will able to clear the confusion surrounding  the concept.