SWT Tech Sharing: Node.js + Redis

72
Node.js @khasathan SWT Tech share 23/12/2013 17/6/2015

Transcript of SWT Tech Sharing: Node.js + Redis

Page 1: SWT Tech Sharing: Node.js + Redis

Node.js @khasathan

SWT Tech share

23/12/201317/6/2015

Page 2: SWT Tech Sharing: Node.js + Redis

Wait ...

Page 3: SWT Tech Sharing: Node.js + Redis

+ Redis 17/6/2015

Page 4: SWT Tech Sharing: Node.js + Redis

Outline●Introduction to Node.js●Node.js Anatomy 101●Node Package Manager (NPM)●Node.js Control Flow●Developments●Deployments●Let's play with workshop

Example codes: https://github.com/khasathan/learning-node

Page 5: SWT Tech Sharing: Node.js + Redis

Warning!

Buffalo gag alert

Page 6: SWT Tech Sharing: Node.js + Redis

JavaScript can run on server side?

Page 7: SWT Tech Sharing: Node.js + Redis

Introduction to Node.js

Page 8: SWT Tech Sharing: Node.js + Redis

What is Node.js?●“Node.js is a platform built on Chrome's

JavaScript runtime for easily building fast, scalable network applications.”

- Nodejs.org

Page 9: SWT Tech Sharing: Node.js + Redis

What is Node.js? (2)

Server side JavaScript

Page 10: SWT Tech Sharing: Node.js + Redis

So what's IO.js?●Node.js fork●Bring ES6 to Node community●NPM compatible

Page 11: SWT Tech Sharing: Node.js + Redis

Why use Node.js?●Non-Blocking I/O●V8 JavaScript engine (like Google Chrome run-

time)●Many modules (> 40,000)●NPM package manager●Use same language as Back-end/Front-end●High concurrent connections

Page 12: SWT Tech Sharing: Node.js + Redis

Node.js use for ...●Modern Web application●Real-time application●Event-based application

Page 13: SWT Tech Sharing: Node.js + Redis

Modern Web Application

Page 14: SWT Tech Sharing: Node.js + Redis

Simple serverhttp = require('http');

http.createServer(function(req, res) {res.writeHead(200, {

'Content-Type': 'text/html; charset=utf-8'});res.write('Simple Node.js Server');res.end();

}).listen(3000, '127.0.0.1');

Page 15: SWT Tech Sharing: Node.js + Redis

Simple server (2)

Page 16: SWT Tech Sharing: Node.js + Redis

Who use Node.js

Page 17: SWT Tech Sharing: Node.js + Redis

Who use Node.js (2)

Page 18: SWT Tech Sharing: Node.js + Redis

Express●Web framework for Node.js●MVC●Template●Pretty URL

$npm install express

Page 19: SWT Tech Sharing: Node.js + Redis

RESTful web service●$npm install restify

Page 20: SWT Tech Sharing: Node.js + Redis

Socket.io●Real-time application●WebSockets

$npm install socket.io

Page 21: SWT Tech Sharing: Node.js + Redis

Socket.io (2)● Socket.io transport

● Websocket● Xhr-polling● Jsonp-polling● Flashsocket● htmlfile

Page 22: SWT Tech Sharing: Node.js + Redis

Real-Time Application

http://thstream-khasathan.rhcloud.com

Page 23: SWT Tech Sharing: Node.js + Redis

Real-Time Application

Entzonet Notification

Page 24: SWT Tech Sharing: Node.js + Redis

Event-Based ApplicationMyCat Back-end

Workers pool - Read un-reviewed documents- Create files queue- Watching status files- Distribute tasks to workers

idle

busy

Files queue

idle

Page 25: SWT Tech Sharing: Node.js + Redis

Shouldn't be used for...●RDBMS, transaction●Heavy computation/Huge data processing

Page 26: SWT Tech Sharing: Node.js + Redis

Blocking vs Non-Blocking●Blocking

Timeline

1

2

3

Page 27: SWT Tech Sharing: Node.js + Redis

Blocking vs Non-Blocking●Non-Blocking

Timeline

1

2

3

Page 28: SWT Tech Sharing: Node.js + Redis

Async Programming● Do next task without waiting for current task

end

fs = require('fs')

fs.readFile('bigfile.txt', function(err, data) {console.log(data)

})

console.log('Read big file')

Page 29: SWT Tech Sharing: Node.js + Redis

Async Programming (2)

$node read-big-file.js

> Read big file> . . . data from big file . . .

Page 30: SWT Tech Sharing: Node.js + Redis

Node.js Anatomy 101

Page 31: SWT Tech Sharing: Node.js + Redis
Page 32: SWT Tech Sharing: Node.js + Redis

Event Loop

console.log('print 1');

compute(data, function(item) {processItem(item);

});

doLargeCompute();

console.log('print 2');

Call stack

Run-time

Event loop

Message queue

DoLargeCompute

Event loop

Page 33: SWT Tech Sharing: Node.js + Redis

Node Package Manager (NPM)

Page 34: SWT Tech Sharing: Node.js + Redis

NPM●Package manager for JavaScript●Dependencies management

Page 35: SWT Tech Sharing: Node.js + Redis

First step with NPM●$npm initname: (learning-node) version: (1.0.0) description: Example code for Node.js tutorialentry point: (index.js) test command: git repository: https://github.com/khasathan/learning-nodekeywords: author: khasathanlicense: (ISC)

●$npm init

Now, we got package.json file

Page 36: SWT Tech Sharing: Node.js + Redis

package.json

Page 37: SWT Tech Sharing: Node.js + Redis

npm install/uninstall

# install locally$npm install <package_name>

# install globally$npm install -g <package_name>

# uninstall locally$npm uninstall <package_name>

# uninstall globally$npm uninstall -g <package_name>

Page 38: SWT Tech Sharing: Node.js + Redis

npm install/uninstall (2)

● “-g” option you may be use “sudo”● Globally installation you can use its like ls, cd, cp, mv etc.

● Locally installation modules are located in node_modules/

Page 39: SWT Tech Sharing: Node.js + Redis

--save options

$npm install - -save redis

Page 40: SWT Tech Sharing: Node.js + Redis

--save-dev options

$npm install - -save-dev mocha

Page 41: SWT Tech Sharing: Node.js + Redis

npm search

$npm search <keyword>

Page 42: SWT Tech Sharing: Node.js + Redis
Page 43: SWT Tech Sharing: Node.js + Redis

Node.js Control Flow

Page 44: SWT Tech Sharing: Node.js + Redis

Node.js Control Flow●Callback●Promise●Event

Page 45: SWT Tech Sharing: Node.js + Redis

Callback vs Promise vs Event●Callback is “Function”●Promise is “Object”●Event is “Event”

Page 46: SWT Tech Sharing: Node.js + Redis

Callbackfs = require('fs');

fs.readFile('bigfile.txt', function(err, data) {console.log(data);

});

console.log('print this');

Page 47: SWT Tech Sharing: Node.js + Redis

Callback (2)fs = require('fs');

var readfile = function (err, data) {console.log(data);

}

fs.readFile('bigfile.txt', readfile);

console.log('print this');

Page 48: SWT Tech Sharing: Node.js + Redis

Callback (3)

fs = require('fs');

var readfile = funtion (err, data) {if(err) throw err;

console.log(data);}

fs.readFile('bigfile.txt', readfile);

console.log('print this');

●How does callback work?

Page 49: SWT Tech Sharing: Node.js + Redis

Callback (4)

fs = require('fs');

var readfile = funtion (err, data) {Cb1(function (data1) {

Cb2(function (data2) {Cb3(function (data3) {

Cb4(function (data4) {... do something ...

});});

});});

};

fs.readFile('bigfile.txt', readfile);console.log('print this first');

●Callback pyramid hell

Page 50: SWT Tech Sharing: Node.js + Redis

Promise●Delay? Future?●Object that holds value

Page 51: SWT Tech Sharing: Node.js + Redis

Promise (2)Fulfilled

The action relating to the promise succeeded.

RejectedThe action relating to the promise failed.

PendingHasn't fulfilled or rejected yet.

SettledHas Fulfilled or rejected.

Page 52: SWT Tech Sharing: Node.js + Redis

Promise

See weather-reporter-promise.js

Page 53: SWT Tech Sharing: Node.js + Redis

Event● Do function when event fired

Page 54: SWT Tech Sharing: Node.js + Redis

Event (2)var mytask = doMyTask();

mytask.on('success', function() {// do this if success

});

mytask.on('progress', function() {// do this if in progress

});

mytask.on('error', function() {// my task if error

});

Page 55: SWT Tech Sharing: Node.js + Redis

Callback vs Promise vs Event●Callback is “Function”●Promise is “Object”●Event is “Event”

Page 56: SWT Tech Sharing: Node.js + Redis

When we use callback?When we use promise?

When we use event?

Page 57: SWT Tech Sharing: Node.js + Redis

Control Flow Libraries●Async●Step●Q●EventEmitter built-in←

Page 58: SWT Tech Sharing: Node.js + Redis

Developments

Page 59: SWT Tech Sharing: Node.js + Redis

Developments●Make your own function with callback●Custom module●Code quality●Unit testing●Logging●Node Version Manager (NVM)

Page 60: SWT Tech Sharing: Node.js + Redis

Make Function with Callback

See function-with-callback.js

Page 61: SWT Tech Sharing: Node.js + Redis

Custom module

See folder custom_module/

Page 62: SWT Tech Sharing: Node.js + Redis

Code Quality$npm install -g jslint

$jslint file.js

Page 63: SWT Tech Sharing: Node.js + Redis

Unit testing●Make small unit works●Unit testing frameworks

●Nodeunit●Mocha●Chai

●TDD/BDD

Page 64: SWT Tech Sharing: Node.js + Redis

Node Version Manager (NVM)

●Use several version of Node, IOjs on one machine

●Website https://github.com/creationix/nvm

Page 65: SWT Tech Sharing: Node.js + Redis

Deployments

Page 66: SWT Tech Sharing: Node.js + Redis

Deployments●Reverse Proxy – use app on port 80●Forever – running app forever

Page 67: SWT Tech Sharing: Node.js + Redis

Reverse Proxy●Nginx - Supported (>= V1.3)●HAProxy - Supported●Apache2 (>=V2.4 with websocket tunnel

module)●For Apache < 2.4 you may be use transport

such as xhr-polling, jsonp-polling

Page 68: SWT Tech Sharing: Node.js + Redis

Forever●Process monitoring for Node.js●Keep application always running

$sudo npm -g forever$forever list$forever [start|stop] app.js$forever --help

Page 69: SWT Tech Sharing: Node.js + Redis

Let's play with workshop

Page 70: SWT Tech Sharing: Node.js + Redis

Redis●Redis Stands for “REmote Dictionary Server”●Key-value NoSQL database●PUBLISH/SUBSCRIBE provided

Page 71: SWT Tech Sharing: Node.js + Redis

Publish/Subscribe●Sometime we know as “Pub/Sub”●Message-oriented middleware system

Channelsubscriber subscriber

subscriber

publisher

subscribersubscriber

Page 72: SWT Tech Sharing: Node.js + Redis

The Funny Scoreboard

https://github.com/khasathan/learning-node