From last two weeks i have been trying to learn node js when i came across this old video by Ryan Dahl where he introduce what is node and what makes it different from others . Till then i was confused about what node really was and how it really works , after all who can explain the creation other than the creator itself , for all who wants to know about the concepts of node i strongly recommend all of you guys to go through this video
Node and the Beast
Ryan says
“node is a bunch of sugar on top of the very complex virtual machine written by google called V8” , so as we all heard about v8 as a javascript engine inside chrome and v8 plays a main role in building desktop application with node , so let me make a reference to hybrid apps and
Apache cordova here so what we really do is we wrap up our html,css and javascript to make mobile applications into a webview which will replicate our web components as a real app inside android,ios and whatnot ; So V8 here provides us with such a platform (webview in case of hybridapp) to run our desktop applications .V8 is basically very deep and high performing ,
node is a bunch of libraries on top of V8 that accesses its functionality and greatness to do networking things and stuffs. No window but process
If you are familiar with java script we know we have a global browser object called “window” through which we can access DOM elements and do stuffs ,but in node you are not associated with a DOM or browser so
you don’t have a window object but you have a process object which is a global variable .
“You can’t sleep in node”
Ryan says
“you cannot sleep in node , simply you are not allowed to do it “ that’s how he explains the non-blocking I/O property of node
Let me explain to you about this :
Above is the code to print hello world by pausing the world to print 2 seconds after hello. First part of the snippet is coded in php and the second in javascript , so what php does is it prints hello waits for 2 seconds in which the state will be blocked or user cant do anything on that state and then releases the state by printing hello .But in node after printing hello it is in a non blocking I/O state that is it can accepts inputs from user , basically it can do other stuffs in the time interval gap that is node wont wait for the world to print after it prints hello instead what happens is it idles , that is after printing hello the program idles , the CPU goes to zero and allows it to do other stuffs in the mean time and come backs when it is necessary .
Another interesting thing about node is it keep tracks of the application so that it exists the application when all of the call backs are finished and not at the middle of something and the plus is that we don’t have to write code to explain the application when to exist.
Below is the benchmark of simple http server created using node we can see how efficient is this from below image:
But where is this useful?
What if you have a interval function for every 2 second and suddenly you have to fetch something else from internet in every 2 second ?
The below program exactly do the same stuff , this is where the about property of node become useful when you got so many stuffs to do asynchronously without disturbing other functions of the application lets say parallel cant we ?
Chat using node
After these discussions Ryan implemented a simple chat system using sockets in node , not much to talk about it but see how simple is it and how beautiful it is to implement this pretty complex task with node callbacks
We can deal well with small programs but what if it comes to Large ones on a long run?
We can say that there will be surely 2 sure questions arising when we aim for big applications
- What about my code maintainability and code structure?
- How about abstraction and hiding things in an efficient way?
For instance we can take an example :
what if we have to get something from the server and write call backs infinitely inside callbacks to save the data to database.This is a complex situation we will face during building an application for real! , so what can be done ? can we indent for ever ? how should i code in such a way that i can eliminate such awkward situation ?
Ryan says the answer is no , we don’t need to indent for ever , then what is the solution ? let see below how it is done :
What we can do by Ryans way is to give the functions name and eliminate the complex situation , so that answers how we can ensure the code maintainability and not worry about indentations
so that answers question 1 , now moving onto the second one
Can we abstract the multiple functions so that we can provide a nice interface to the user hiding the database call backs happening in background?
Yes we can the idea is to create a function that will expose your http get function to the user and to pass the callback function throughout other functions so that it will execute after all other callbacks is finished , below is the example for the same :
Thats most of the things i learned and understood from the video , the video is quite interesting and Ryan talks many more things which are important and pretty easy to understand and make a good understanding of node and how can it be used in an efficient way, node is much advanced as of now and you can get many easy tutorials and screen casts which will guide you well into a good node programmer and help you build beautiful apps , but i suggest you guys to start your journey with this video so you can understand the concept of node well, thanks