Sometime back I had attended a session conducted by K-MUG and one of the session was ‘How to integrate Node.JS with Windows Azure’. Shiju who is a MVP took the session really good and as the result I started learning the Node.JS technology.I am not the right person to talk about the technology and compare its merits with other existing technologies. But the idea of asynchronous event driven programming really interested me as that is something new,comparing with the existing programming approach.(I had worked with threads,events,callbacks etc…in .Net.But Node.JS seems fully leveraging the async)Some points what I understood about Node.JS
- It has or starts its own webserver like IIS and Apache
- Even IIS can relay the request to NodeJS (node.exe) via IISNode module.
- NodeJS can be used to develop web sites as well as web services.
- How Node will handle different protocols / compete with WCF is still confusing me. May be there will be extensions for handling that. Don’t ask why Node.JS need to support .Net specific remoting via net.tcp because I don’t want to write services twice for my external clients and internal clients.
- Everything except our code runs in different thread.For example all the IO and DB related code runs in different thread. Our code always runs in single thread.
- The above can be understood by an example of 2 concurrent requests. If there are no IO or other parallelizable requests Node will process requests one by one.But if there is an IO operation in first request ,Node will put that IO operation into another thread and takes the second from the event loop. This gives us a virtual feeling of requests/our code being executed in parallel. This is achieved by using 2 libraries called libev & libeio
- Better for simple web site / ReST kind of service applications. ie Only if our code which is executing in the single thread completes as soon as possible. If we can route the long running process to another thread such as how IO, DB,Network related operations are performed, its fine.
- Its production ready and so many big busiest business sites are using it. Check out NodeJS site for the list of big users.
This is just a code snippet to prove that our code is not running in parallel in Node.JS. Main reason is Node.JS user code don’t have capability to create it’s own threads. Below is a code snippet which puts a delay of 5 seconds in a normal request. If you hit the URL (http://localhost:8000) from your browser you will see that the response is coming after 5 seconds.The response in browser will be.
Now Open 2 tabs in your browser and hit the same url simultaneously. The result will be
It clearly says that the Node.JS is single threaded from our application point of view and it needs to wait to complete current user code to take another request.If my delay code was to fetch contents from file it should have processed differently as the I/O code will go into another thread.Think about ASP.Net.If you write the same code in ASP.Net and hit simultaneously from 2 browser tabs, the results would be
Ok. What about seeing the Node.JS code execution in parallel.I am altering the delay code to execute a sql query in SQL server. Query is nothing but a WAIT FOR DELAY statement.
If you are not familiar with how to setup SQL Server with NodeJS please refer the below link.
After this I tried hitting the url from different browser tabs and I got the below output.
Process started at :Fri Jul 06 2012 20:45:51 GMT+0530 (India Standard Time) DB Execution (WAIT FOR DELAY "000:00:10") started @ 20:45:51 DB execution completed @20:46:01
Process started at :Fri Jul 06 2012 20:45:53 GMT+0530 (India Standard Time) DB Execution (WAIT FOR DELAY "000:00:10") started @ 20:45:53 DB execution completed @20:46:03
Hope you understood the output. The second request was able to get into execution only because the first request entered into SQL execution which happens in different thread. For more details refer the below link.
The programming model
All the operations which are to be done after async calls like I/O calls needs to be inside the event handler / callbacks. in simple words nested callbacks. So lets see how to read a file after a sql database call where the file read is depend on the first sql execution result.
Simple isn’t it? Lets consider one more scenario where there is parallelism in SQL and File operations and another operation needs to be performed after these 2 operations.
But this needs an additional check to ensure that both the operations are completed.ie NodeJS don’t have native beautiful way of handling multiple async callbacks and do operations based on that.So inject our own logic.Keep 2 variables to hold the return state and in the post processing function check the variables.
More links below.