+ All Categories
Home > Documents > Real Time App with Node.js

Real Time App with Node.js

Date post: 12-May-2015
Category:
Upload: jxck-
View: 6,345 times
Download: 1 times
Share this document with a friend
Description:
Slide for Seminar at JUS(Japan UNIX Society) 2011/07/22@sendagayaTutorial about Real Time App by Express + [email protected] some tips.
36
Real Time Appwith 2011/07/22@jus id: Jxck 1 2011727日水曜日
Transcript
Page 1: Real Time App with Node.js

“Real Time App”with

2011/07/22@jusid: Jxck

1

2011年7月27日水曜日

Page 2: Real Time App with Node.js

Jack

•id: Jxck•group: #nodejs_jp•twitter: jxck_•blog: http://d.hatena.ne.jp/Jxck•tubmlr: http://jxck.tumblr.com/•commit: jsonengine•Writing Node.js Book(now)

2

2011年7月27日水曜日

Page 3: Real Time App with Node.js

Real Time App with Node.js• Node でリアル(タイムアプリ)を充実させる。• リアルタイムアプリ

✓ アプリを作る = Express✓ リアルタイムにする = Socket.IO

• Node でリアルタイムアプリってどうやって作るの?

• Node が生かせる場面とは?• and more

3

2011年7月27日水曜日

Page 4: Real Time App with Node.js

installing node

4

2011年7月27日水曜日

Page 5: Real Time App with Node.js

Node.js を始めるには言語 環境管理 パッケージ管理

Ruby rvm gem

Python Virtualenv PyPI

Perl Perlbrew CPAN

node nave/nvm npm• https://github.com/creationix/nvm• https://github.com/isaacs/nave

5

2011年7月27日水曜日

Page 6: Real Time App with Node.js

require(‘express’);

6

2011年7月27日水曜日

Page 7: Real Time App with Node.js

Express ?

•http://expressjs.com/•Web Application FW•Sinatra Inspired•TJ(http://twitter.com/tjholowaychuk)•Light & Fast•Built on Connect

7

2011年7月27日水曜日

Page 8: Real Time App with Node.js

Express Skeleton

> npm install express -g// now you can use express command> express -s -t ejs sample// sample using session and ejs template> cd sample && npm install// resolve dependencies

8

2011年7月27日水曜日

Page 9: Real Time App with Node.js

Skeleton$ tree

.!"" app.js!"" logs!"" package.json!"" pids!"" public#   !"" images#   !"" javascripts#   $"" stylesheets#   $"" style.css$"" views !"" index.ejs $"" layout.ejs

7 directories, 5 files

$ node app.js node app.js Express server listening on port 3000 in development mode

9

2011年7月27日水曜日

Page 10: Real Time App with Node.js

/** * Module dependencies. */var express = require('express');

var app = module.exports = express.createServer();

/** snip **/

app.listen(3000);console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

app.js10

2011年7月27日水曜日

Page 11: Real Time App with Node.js

// Configurationapp.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieParser()); app.use(express.session({ secret: 'your secret here' })); app.use(app.router); app.use(express.static(__dirname + '/public'));});

app.configure('development', function(){ app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); });

app.configure('production', function(){ app.use(express.errorHandler()); });

$ NODE_ENV=production node app.js$ NODE_ENV=development node app.js

app.js11

2011年7月27日水曜日

Page 12: Real Time App with Node.js

<!DOCTYPE html><html> <head> <title><%= title %></title> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> <%- body %> </body></html>

<h1><%= title %></h1><p>Welcome to <%= title %></p>

// Routes

app.get('/', function(req, res){ res.render('index', { title: 'Express' });});

layout.ejs

index.ejs

app.js

12

2011年7月27日水曜日

Page 13: Real Time App with Node.js

app.get('/users', function(req, res) { res.render('users/index', { title: 'index', users: users });});

app.get('/users/new', function(req, res) { res.render('users/new', { title: 'new' });});

app.post('/users', function(req, res) { var username = req.param('username'); var email = req.param('email'); users.push({ username: username, email: email }); res.redirect('/users/');});

app.get('/users/:id', function(req, res) { var id = req.param('id'); res.render('users/show', { title: 'show', id: id, users: users[id] });});

app.get('/users/:id/edit', function(req, res) { var id = req.param('id'); res.render('users/edit', { title: 'edit', id: id, users: users[id] });});

app.put('/users/:id', function(req, res) { var id = req.param('id'); var username = req.param('username'); var email = req.param('email'); users[id] = { username: username, email: email }; res.redirect('/users/');});

app.delete('/users/:id', function(req, res) { var id = req.param('id'); users.splice(id, 1); res.redirect('/users/');});

// User Datavar users = [{username: 'Jxck', email: '[email protected]'}];

Sample Routinglike Rails Scaffold

13

2011年7月27日水曜日

Page 14: Real Time App with Node.js

WebSocket

14

2011年7月27日水曜日

Page 15: Real Time App with Node.js

WebSocket15

2011年7月27日水曜日

Page 16: Real Time App with Node.js

very easy chat16

2011年7月27日水曜日

Page 17: Real Time App with Node.js

require(‘socket.io’);

17

2011年7月27日水曜日

Page 18: Real Time App with Node.js

Socket.IOSupported transports• WebSocket• Adobe® Flash® Socket• AJAX long polling

• AJAX multipart streaming• Forever Iframe• JSONP Polling

Supported browsers• Internet Explorer 5.5+• Safari 3+•Google Chrome 4+

• Firefox 3+•Opera 10.61+• iPhone Safari• iPad Safari•Android WebKit•WebOs WebKit

18

2011年7月27日水曜日

Page 19: Real Time App with Node.js

<h1><%= title %></h1><p>Welcome to <%= title %></p>

<input id="msg" type="text"></input><input id="ok" type="button" value="ok"></input>

<ul id="display"></ul>

<!DOCTYPE html><html> <head> <title><%= title %></title> <link rel='stylesheet' href='/stylesheets/style.css' /> <script src='jquery-1.6.1.min.js'></script> <script src='/socket.io/socket.io.js'></script> <script src='/javascripts/sample.js'></script> </head> <body> <%- body %> </body></html>

layout.ejs

index.ejs

19

2011年7月27日水曜日

Page 20: Real Time App with Node.js

$(function() { var socket = io.connect('http://localhost'); var $msg = $('#msg') , $ok = $('#ok') , $display = $('#display') ;

socket.on('msg push', function (data) { var $li = $('<li>').text(data); $display.append($li); });

$ok.click(function() { socket.emit('msg send', $msg.val()); });});

// Socket.IO

var io = require('socket.io').listen(app);

io.sockets.on('connection', function (socket) { socket.on('msg send', function (data) { socket.emit('msg push', data); socket.broadcast.emit('msg push', data); });});

app.js

sample.js

20

2011年7月27日水曜日

Page 21: Real Time App with Node.js

Use Case ?

21

2011年7月27日水曜日

Page 22: Real Time App with Node.js

I/O Heavy > CPU Heavy

22

2011年7月27日水曜日

Page 23: Real Time App with Node.js

I/O Heavy

•Network I/O✓ WebSocket✓ Ajax

•Disk I/O✓ File Uploader

23

2011年7月27日水曜日

Page 24: Real Time App with Node.js

Real Time Web ?

24

2011年7月27日水曜日

Page 25: Real Time App with Node.js

25

2011年7月27日水曜日

Page 26: Real Time App with Node.js

http://bit.ly/node-ssp

26

2011年7月27日水曜日

Page 27: Real Time App with Node.js

Scale

27

2011年7月27日水曜日

Page 28: Real Time App with Node.js

Multi Process / IPC

•node-webworker✓ websocket

•Cluster✓ JSON-RPC

28

2011年7月27日水曜日

Page 29: Real Time App with Node.js

Sharing WebSocket

•Redis Pub/Sub

http://bit.ly/node-ssp-scale

29

2011年7月27日水曜日

Page 30: Real Time App with Node.js

SocketStream !?

30

2011年7月27日水曜日

Page 31: Real Time App with Node.js

SocketStream• Single-page Application• Socket.IO• CoffeeScript• Jade & Stylus• Underscore.js• jQuery & jQuery templates• Auto minifies• Scale by Redis Pub/Sub• Authentication• etc

https://github.com/socketstream/socketstream

31

2011年7月27日水曜日

Page 32: Real Time App with Node.js

more

32

2011年7月27日水曜日

Page 33: Real Time App with Node.js

Test•Assertion✓Assert✓Should

•Testing FW✓Expresso✓Node-Unit✓Vows

•Utility✓Sinon.js✓node-jscoverage

http://bit.ly/tng1-node-test

33

2011年7月27日水曜日

Page 34: Real Time App with Node.js

Node Knock Out

•Node Knock Out

• 48-hour coding contest featuring node.js.

• Schedule✓ 7/13 - 8/20 : Registration

✓ 8/27 - 8/29 : Competition

✓ 8/29 - 9/5 : Judging

http://nodeknockout.com/

34

2011年7月27日水曜日

Page 35: Real Time App with Node.js

Question ?

35

2011年7月27日水曜日

Page 36: Real Time App with Node.js

Jack

Thank You ;-)

36

2011年7月27日水曜日


Recommended