Tech io nodejs_20130531_v0.6

15
Tech I/O Node.js Server Side JavaScript Ganesh Kondal May 31, 2013

description

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

Transcript of Tech io nodejs_20130531_v0.6

Page 1: Tech io nodejs_20130531_v0.6

Tech I/O Node.js Server Side JavaScript

Ganesh Kondal May 31, 2013

Page 2: Tech io nodejs_20130531_v0.6

Agenda

Background

Overview & Introduction

Sample Code

• Event Model

• Non Blocking I/O

• Modules

Key Concepts

Applicability

In the Industry

2

Page 3: Tech io nodejs_20130531_v0.6

• 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

Page 4: Tech io nodejs_20130531_v0.6

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

Page 5: Tech io nodejs_20130531_v0.6

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

Page 6: Tech io nodejs_20130531_v0.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

Page 7: Tech io nodejs_20130531_v0.6

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

Page 8: Tech io nodejs_20130531_v0.6

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!

Page 9: Tech io nodejs_20130531_v0.6

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

Page 10: Tech io nodejs_20130531_v0.6

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 World\n'); }) .listen(5000, "127.0.0.1");

var net = require('net'); net.createServer(function (socket) { socket.write("Echo server\r\n"); socket.pipe(socket); }) .listen(9000,"127.0.0.1");

Page 11: Tech io nodejs_20130531_v0.6

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 @ nmpjs.org

• NPM (Node Package Manager) comes bundled with Node.js installation.

11

Page 13: Tech io nodejs_20130531_v0.6

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

Page 14: Tech io nodejs_20130531_v0.6

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 :

http://www.ebaytechblog.com/2013/05/17/how-we-built-ebays-first-node-js-application/

For more refer - http://nodejs.org/industry/

14

Page 15: Tech io nodejs_20130531_v0.6

Thank You