Tech io nodejs_20130531_v0.6


Published on

Collated material to serve as a quick introduction of NodeJS to the team

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Tech io nodejs_20130531_v0.6

  1. 1. Tech I/O Node.js Server Side JavaScript Ganesh Kondal May 31, 2013
  2. 2. Agenda Background Overview & Introduction Sample Code • Event Model • Non Blocking I/O • Modules Key Concepts Applicability In the Industry 2
  3. 3. • Quick collation of material from various allowed sources – net, books & slides – to introduce the team to Node.JS • To provide a glimpse of Node.JS About For Folks who don’t know anything about Node.JS
  4. 4. Background • Node.js runs on V8 Javascript Engine. • V8 is Google’s open source engine; written in C++. • Created by Ryan Dahl in 2009. • Runs on Linux systems and Windows systems. • Per Node.js • IO is too costly • Thread per connection is too memory intensive !! (Apache creates one thread per connection) 4
  5. 5. Introduction • Node.js is ‘server-side JavaScript’. • High-performance network applications framework, • Well optimized for high concurrent environments. • Command line tool to execute nodeJS code. • Written with 40% JS and 60% C++ code. • Lightweight - Uses an event-driven, non-blocking I/O model. • Event-loops / Non-blocking IO via JavaScript’s callbacks. • Programs for Node.js are written in JavaScript [no DOM implementation ] • Everything inside Node.js runs in a single-thread. • Yes!! Its single threaded. Below code will block the server for a second. • All but your code is multi-threaded !!!  5
  6. 6. ‘Hello Node.js’ • Type your JS code, open cmd/terminal and type this: ‘node FILE_NAME.js’ • To execute the code • ‘hello world’ var sys = require(“sys”); setTimeout(function(){ sys.puts(“world”);}, 3000); sys.puts(“hello”); //it prints ‘hello’ first and waits for 3 seconds and then prints ‘world’ 6
  7. 7. Event Loops 7 Event-loops are the core of event-driven programming, almost all the UI programs use event-loops to track the user event, for example: Clicks, Ajax Requests etc. Client Event loop (main thread) C++ Thread pool (worker threads) Clients send HTTP requests to Node.js server An Event-loop is woken up by OS, passes request and response objects to the thread-pool Long-running jobs run on worker threads Response is sent back to main thread via callback Event loop returns result to client
  8. 8. Non Blocking I/O • Traditional I/O • Non-traditional, Non-blocking I/O 8 var result = db.query(“select name from student”); computeStudentExamResult(result); //wait for result! informResults(); //execution is blocked! db.query(“select name from student”,function (result){ computeStudentExamResult(result); //wait for result! }); informResults(); //executes without any delay!
  9. 9. Few things with Node.js • You can create an HTTP server and print ‘hello world’ on the browser in just 4 lines of JavaScript. • You can create a TCP server similar to HTTP server, in just 4 lines of JavaScript. (Example included) • You can create a DNS server. • You can create a Static File Server. • You can create a Web Chat Application like GTalk in the browser. • Node.js can also be used for creating online games, collaboration tools or anything which sends updates to the user in real-time. 9
  10. 10. Servers… in 3 lines of code 10 • Create HTTP Server and print ‘Hello World’ : • TCP server on port 9000 that echoes the request: var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }) .listen(5000, ""); var net = require('net'); net.createServer(function (socket) { socket.write("Echo serverrn"); socket.pipe(socket); }) .listen(9000,"");
  11. 11. Node Modules • Node.js heavily relies on modules, in previous examples require keyword loaded the http & net modules. • Creating a module is easy, just put your JavaScript code in a separate js file and include it in your code by using keyword require, like: var mod = require(‘./modulex’); • Libraries in Node.js are called packages and they can be installed by typing npm install “package_name”; //package should be available in npm registry @ • NPM (Node Package Manager) comes bundled with Node.js installation. 11
  12. 12. Applicability • Node.js is good for creating streaming based real- time services, web chat applications, static file servers etc. • If you need high level concurrency and not worried about CPU-cycles. • If you are great at writing JavaScript code because then you can use the same language at both the places: server-side and client-side. • High productivity (as long as the developer understands/thinks in terms of callbacks, async I/O) • Node.js principle – IO is costly. • g-the-node-js-event-loop/ 12
  13. 13. Not Applicable For 13 • Computation intensive tasks on server side, as the event-loops are CPU hungry. • Still in beta – so be cautious !! • Can’t guarantee backward compatibility. Not a 1.0 • No match for enterprise level application frameworks like Spring(java), Django(python), etc. • Not battle tested yet!!!
  14. 14. Node.js in the Industry • LinkedIn : • On the server side, our entire mobile software stack is completely built in Node. One reason was scale. The second is Node showed us huge performance gains. • Clouds 9 IDE • eBay : node-js-application/ For more refer - 14
  15. 15. Thank You