node.js primer

15
Node.js That's what happens when JavaScript runs on Server too By Swapnil Mishra

Upload: swapnil-mishra

Post on 31-Oct-2014

1.438 views

Category:

Education


5 download

DESCRIPTION

This is the talk i gave in my office to introduce/highlight advantages of node.js.

TRANSCRIPT

Page 1: Node.js primer

Node.js

That's what happens when JavaScript runs on Server too

By Swapnil Mishra

Page 2: Node.js primer

But Javascript on server ?

Page 3: Node.js primer

• 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

Page 4: Node.js primer

• 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 ?

Page 5: Node.js primer

• 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 !!

Page 6: Node.js primer

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

?

Page 7: Node.js primer

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

?

Page 8: Node.js primer

• 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

Page 9: Node.js primer

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

Page 10: Node.js primer

Apache vs NGINX

Page 11: Node.js primer

Apache vs NGINX

Page 12: Node.js primer

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

Page 13: Node.js primer

• 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 World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/');

Now i got it

Page 14: Node.js primer

• 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 ?

Page 15: Node.js primer

Thank You !!!

Time for a quick demo