3. • Powerful JavaScript engine which powers Google Chrome browser
and Chrome OS.
• Can also be considered as a Virtual Machine.
• Node.js runs on the top of V8.
• Utilizes the networking and parsing features which are done elegantly
in V8.
V8 made it possible
4. • There are differences in the JavaScript which runs in browser vs
node.js.
e.g. There are no "window" or "document" objects.
• Things which have been taken from V8 are asynchronous/non-
blocking nature of JavaScript, Syntax, Data Structures(most of them).
Which means everyones favorite closure/anonymous functions will
work here too.
• Things which have been added into it are Timers, Processes, Events,
Buffers, Streams, Crypto, File Systems, HTTP, HTTPS and many more.
Wait, so you are saying this node.js is same
as the javascript which runs in browser ?
5. • Model is evented rather than threaded.
• Using an event loop, you ask it to do something and it will get back to
you when its done.(we will see an example later)
• Conventional languages are run in threaded environment where they
create threads and just loop/pool in to wait for the result. Thats not
the case with node.js.
• Because of the evented nature it can perform Non-Blocking Network
I/O.
Oh cool tell me more about it !!
6. Example of Blocking I/O
public INeedData {
public Data getMeSomeData(queryParam){
Data toBeReturned = db.query(queryParam);
iAmBlockedTillQueryExecutes();
return toBeReturned;
Blocking I/O
}
}
// get data performs blocking I/O call because of which
// iAmBlockedTillQueryExecutes() is blocked
Why should i care about Non-Blocking I/O
?
7. Think about what we really do on web server.
• Grab Files
• Call Databases
• Pull from caches
• Wait on other connections
It's all I/O and we know that I/O latency of
network is highest.
Why should i care about Non-Blocking I/O
?
8. • Totally and so does web-servers like Apache, IIS.
• But threading is not free.
• There are context switches and executions stacks take
memory.
What should we do then ?
Hey but that's exactly why i use thread
9. Use a single thread. Do little pieces of work and do them quickly.
But single thread !!!
We have been using multithreading from ages
when UI used to be in "Java Swing" !!!
Are you crazy ?
Can it handle ?
Event loop comes to the rescue
12. The difference?
• Apache uses one thread per connection.
• NGINX does not use threads. It uses an event loop.
So basically we have to change our programming model to something
where we could easily write non-blocking code.
public INeedData {
public Data getMeSomeData(queryParam){
Data toBeReturned = db.query(queryParam,function(){
return
toBeReturned;});
iAmNotBlockedTillQueryExecutes();
}
}
Apache vs NGINX
13. • Node.js is a set of bindings for writing high-performance network
servers.
• Exposes only non-blocking I/O API's
• Stops us writing code that behaves badly.
Below is code for a simple http server in Node.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Now i got it
14. • Node.js is being used by many companies including big names like
Linkedin, WalMart in fact Linkedin's mobile backend is totally on
node.js.
• Excellent package manager npm(Node Package Manager).
• Installing packages: npm install <package>
• npm can also be used for dependency management just like maven.
• One more reason for Node.js's popularity is huge base of javascript
developers who can now write backend code too.
Production Usage ?