Sunday, January 26, 2014

Enter the Node-Hood

So today we will explore the node neighborhood in order to see what awesome goodies lie in the node-land. Ok I am no expert artist, but I remember well when I jot down something I learn into a diagram and utter those famous words “A picture speaks a thousand words”. And of course I love to learn by adding some humor to it which again makes me remember things better. So this is my lame attempt in doing just that to introduce the world of Node to anyone who might be interested.


If you are a node pro, I will save you the time and ask you to close this tab right now… Ah so you did not close it, great, then come on in. So let us see what we will find today on node-land. Following is a picture I just drew in order to emphasize what we will focus on today.



So this is the node home as of now for me. You are greeted with a door mat which leads to the site where you can get more information on the same. And as always you can see just right to the door, there is a CD( who uses CDs nowadays, I know) which states the famous command you will encounter many a times when you are working with node.
npm install <module_name>
Add a –save to the end of the command, and it will save the module you just installed in your package.json file. More on that in the future when we discuss web development with express.
The today’s special bored in the house highlights some core modules that you will find when working with node. They include;
  •  fs
  • crypto
  • process
  • http


This is not the whole list and you will indeed find many more as we go along with the journey through nodejs.

So we have a friend inside our nodehood who is asking for a cool beer. His best friend as you can see is approaching the Node Fridge to get him one. In node land, you can ask for external elements if they are willing to expose their functionality. And this is done through the use of exports. Let us see more closely what is happening inside the Node Fridge;


 
var  drink = function(name){
 return "Cool "+name;
}

module.exports.drink = drink;


So the Node Fridge is exposing a drink function which returns any drink that is passed into it with an added coolness to the drink. Of course there is no point in the fridge having the cold drinks if no one is going to be drinking it, and hence we use a mode.exports at the end to expose this coolness to the whole world.

So his friend has to open up and drink this up. Since he requested this first from his friend, he added the following;


 
var beer = request('./nodefridge');

console.log("Chilling with a ”+beer.drink("beer");


As he is making the request for the drink from the same house itself, he can do as it has been highlighted in the above code. But if he was asking this from the backyard of the house, then he will need to request for the same as follows;


 
var beer = request('./backyard/nodefridge');

The assumption here is that the nodefridge.js is residing in the backyard directory. You can download this simple example from here and run it using node main.js.

So today we will cover how file streams work in the node world. The main point here is that node file streams work with the use of Event Emitters which is a way of notifying results as and when an event occurs. It is slightly different from callbacks where you can get partial results even if an error occurs midway through. It acts as more of a Pulish/Subscribe model as opposed to the Request/Reply  model in callbacks.


Before diving into how streaming works in Node, let us see how they actual work within this new found Event emitter paradigm. Again a little comic strip will better help you understand the interaction between the Readable Streams and Writtable Streams. And no surprises with their names where RS Dude is representing Readable Streams and WS Dude will be representing Writable Streams. Let us see how this turns out;




The story is self explanatory I believe. But if you want to go all technical, here is it in technical terms.

·         The read stream will pipe itself to the writable stream where by the writable stream will it’s write method internally.
·         As it keeps on writing, the write() method can send false which means it cannot accept any more data for the moment. At this moment, the readable stream will call it’s pause() method.
·         After the writable stream is ready again, it will call it’s drain() method and in turn the readable stream will call it’s resume() method and the data will again start flowing.
·         When the end of the stream is reached by the readable stream, it will call it’s end() method and the writable stream will call it’s end() method as well and close the stream.

I have included a sample on using readable streams which is available in the same download I mentioned earlier. You can run the examples as follows;
node readable.js
node wriestream.js

Note that before you run this, please run npm install request as this example requires this module.


No comments:

Post a Comment