Node.js

437 views

Published on

Published in: Software, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
437
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Node.js

  1. 1. NODE JS by Gyuri Horak
  2. 2. WHAT IS NODE.JS “Node has a clear purpose: provide an easy way to build scalable network programs. It is not a tool for every problem. Do not write a ray tracer with Node. Do not write a web browser with Node. Do however reach for Node if tasked with writing a DNS server, DHCP server, or even a video encoding server.” Ryan Dahl
  3. 3. NODE.JS ● server-side javascript environment ● asynchronous event-driven model ● based on Google's V8 javascript engine ● open source, controlled by Joyent ● well documented ● lot of 3rd party modules
  4. 4. ASYNCHRONOUS? ● like Python's Twisted or Ruby's EventMachine ● the event loop is hidden from the user ● non-blocking I/O (network, file, etc.) ● developers should think async
  5. 5. ASYNCHRONOUS! mysql_select_db('db'); $result = mysql_query("SELECT * FROM table"); while ($row = mysql_fetch_array($result)) { echo $row['data']; } echo "finished"; var mysql = require('mysql'), client = mysql.createClient({ user: 'user', password: 'password' }); client.query('USE db'); client.query('SELECT * FROM table', function selectCb(err, result, fields) { console.log(result); } ); console.log("finished");
  6. 6. NO THREADS ● a node.js application is single threaded ● child_process.fork() ● I/O doesn't block, but you can: for (var i = 0; i < 10000; i++) { heavy_calculate_PI(); } for (var i = 0; i < 10000; i++) { setTimeout(heavy_calculate_PI, 0); }
  7. 7. MODULES ● CommonJS Modules proposal var hello = exports; hello.say = function (name) { console.log("Hello "+name); } var hello = require('./hello'); hello.say('Sanyi'); // "Hello Sanyi!" ● vs. AMD [Asynchronous Module Definition]
  8. 8. MODULES ● npm - node package manager ● more then 1000 listed on the wiki ● ~5000 repositories on github related to node.js Web frameworks (MVC like fws, static file servers, middlewares), database drivers (mysql, pgsql, sqlite, mongodb, ...), templating, css engines, CMSs, SSL/crypto fws, SMTP, TCP/IP, RPC, web socket, message queues, class systems, testing fws, parsers (json, xml, command line, parser generators), debugging utilities, compression, graphics, payment gateways, i18n/l10n, coffeescript, full text search, ...
  9. 9. HTTP MODULE var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/'); $ node hello.js Server running at http://127.0.0.1:1337/
  10. 10. SPEED ● Google V8 (used in Chrome as well), JIT ● may perform better then not optimized C++ (performance test) ● ~25 times faster than PHP (~10* CPython, ~2.5* PyPy) ● http module outperforms apache, sometimes even nginx ● typical setup: multiple node.js instances running behind an nginx (or node.js) proxy
  11. 11. SPEED OF HELLO.JS 50000 requests ● 1 client: 2211 r/s (apache: 2991) (nginx: 4009) ● 100 clients: 8088 r/s (apache: 12520) (nginx: 14021) ● 1000 clients: 6924 r/s (apache: crbp, 5693, 1356 failed) (nginx: 11603, error: 235) ● 10000 clients: 5786 r/s (C10k problem?) (apache: crbp, 3956, 6421 failed) (nginx: 5991, error: 23137) ● 20000 clients: 4559 r/s ... (apache: crbp, 2399, 9552 failed) (nginx: -)
  12. 12. jsdom ● DOM implementation for node.js ● almost everything works ● YUI 3, jQuery, ... ● unit testing web applications ● rendering and manipulating content on server side
  13. 13. HOSTING ● easy to install ○ very few dependecies ○ ./configure && make ● easy to manage ○ no need for webserver ● Lot of managed node.js hosting providers: Heroku, no.de, CloudFoundry, dotCloud, ...
  14. 14. EXAMPLE PROJECTS ● Calipso - CMS based on node.js ● 64squares - Real time chess on facebook ● Hummingbird - real time visitor statistics ● Kraken.io - image optimizer ● Word2 - MMO word game ● ...

×