node.js introduction
DESCRIPTION
in Joomladay 2013 BangkokTRANSCRIPT
node.jsintroduction of server side javascript
Saturday, November 23, 13
Sira Sujjinanont@hunt
Managing DirectorWebiz Co.,Ltd.
CTO Jitta.com
15 years inWeb development
Saturday, November 23, 13
Web Development Journey
• Static HTML
• PERL/CGI (2 years)
• PHP (11 years)
• CMS > Drupal, Wordpress, Joomla
• Framework > Yii, Zend, CodeIgniter, …
• What’s next?!?
Saturday, November 23, 13
PHP bad parts
• Consume a lot of memories
• Cannot do Realtime Application
• Cannot deal direct with protocol layer
• No parallel processing
Saturday, November 23, 13
What I need in the next?
• Easy to learn (for me and for my team)
• Fast / Scalability
• Allow us to dig deep in engineering
• New technology! New boundary!
• Open source with great community
Saturday, November 23, 13
And node.js came up!(server-side javascript)
but we need to dig it deep!!
Saturday, November 23, 13
Why Javascript?
• Easy to learn and understanding!
• Front-end coder can do backend immediately!
• Javascript Engine war!(huge changed on web industry)
• Non-blocking I/O
• One language on both front and back!!!
Saturday, November 23, 13
Server-side javascript
• Netscape LiveWrite 1996
• Rhino 1997
• and over 50 platforms!
Saturday, November 23, 13
Many problems!
• Slow engines
• Lag of interest in Javascript language(until 2005… ajax trend!)
• Great competitors platform and language
Saturday, November 23, 13
• Created by Ryan Dahl in 2009easily building fast, scalable network applications
• Google V8 Javascript Engine(Same technology in Google Chrome)
• NPM Package Manager(Come by default great as it should be: 48,362 modules)
• Realtime, Command line, Many tools
Saturday, November 23, 13
When do we need a web?
• Download web server and install
• Install PHP and maybe database
• Configuration….
• And now we can put our code on it!
Saturday, November 23, 13
var http = require('http');http.createServer(function (req, res) { res.writeHead(200); res.end("Hello World!\n");}).listen(3000);console.log('Server running at http://localhost:3000/');
See Node.js way!(create a web server, just in 6 lines of code)
2
1 Download and click installapt-get install nodejs
Saturday, November 23, 13
var http = require('http');http.createServer(function (req, res) { res.writeHead(200); res.end("Hello World!\n");}).listen(3000);console.log('Server running at http://localhost:3000/');
It just works!Web server and ready fora huge traffic :)
even 10,000/reqs
Saturday, November 23, 13
var a = db.query(“SELECT * FROM users”);console.log(‘Result a:’, a);
var b = db.query(“SELECT * FROM documents”);console.log(‘Result B:’, b);
Blocking I/O
Saturday, November 23, 13
var a = db.query(“SELECT * FROM users”);console.log(‘Result a:’, a);
var b = db.query(“SELECT * FROM documents”);console.log(‘Result B:’, b);
Blocking I/O
wait
wait
Waste cpu cycles from waiting…other part of code, they need to wait too!
Saturday, November 23, 13
var a = db.query(“SELECT * FROM users”);console.log(‘Result a:’, a);
var b = db.query(“SELECT * FROM documents”);console.log(‘Result B:’, b);
Blocking I/O
sum ( a + b)
wait
wait
Saturday, November 23, 13
var a = db.query(“SELECT * FROM users”, function(result){ console.log(‘Result a:’, a); });
var b = db.query(“SELECT * FROM documents”, function(result){ console.log(‘Result b:’, ab); });
Non Blocking I/O
Saturday, November 23, 13
var a = db.query(“SELECT * FROM users”, function(result){ console.log(‘Result a:’, a); });
var b = db.query(“SELECT * FROM documents”, function(result){ console.log(‘Result b:’, ab); });
Non Blocking I/O
max ( a, b )
run parallel
run parallel
Saturday, November 23, 13
Benchmark
nginx (non blocking i/o) apache (blocking i/o)
Saturday, November 23, 13
Suitable Applications• Single page app
• Full web app
• Realtime appchat, notification, games
• Crawler, Bot
• Streaming
• File uploading
Saturday, November 23, 13
Who using now?
and thousand thousand more…including me... Molome ....
Saturday, November 23, 13
You should try!
• It’s not hard to start if you familiar with Javascript
• If you looking for another language
• It’ll change the way you do a web
• And it’s FUN!!
Saturday, November 23, 13
Start now!http://github.com/hunt/seventiestemplate for node.js web applicationby me!
https://github.com/jlleblanc/nodejs-joomlamodule for Joomla
Saturday, November 23, 13
Q&AFree to ask!
Saturday, November 23, 13