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”
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 :
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