• Save
node.js - Eventful JavaScript on the Server
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

node.js - Eventful JavaScript on the Server

on

  • 3,168 views

Presentation made on January 2011 about node.js. This technology was used to be the main technology behind the API on "Guia VIVO TV" (codename TVSTAR) with MongoDB.

Presentation made on January 2011 about node.js. This technology was used to be the main technology behind the API on "Guia VIVO TV" (codename TVSTAR) with MongoDB.

Statistics

Views

Total Views
3,168
Views on SlideShare
3,079
Embed Views
89

Actions

Likes
3
Downloads
0
Comments
0

4 Embeds 89

http://wupsbr.com 82
https://twitter.com 3
http://www.linkedin.com 3
http://nodeslide.herokuapp.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

node.js - Eventful JavaScript on the Server Presentation Transcript

  • 1. node.js - Eventful JavaScript on the ServerBy David Ruiz - @wupsbrCentro de Inovação do grupo Telefônica Brasil
  • 2. What is node.js?In a nutshell, it’s JavaScript onthe server sideCreated by Ryan Dahl (@ryah) in2009Based on the Google V8JavaScript engine + Evented I/OPerformance is king: Property access through hidden classes Machine code Garbage collection
  • 3. Want a HTTP Server?var http = require(http);http.createServer(function(req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Welcome to the HTTP Server!);}).listen(4000);
  • 4. Want a TCP Server?var tcp = require(net);tcp.createServer(function(socket) { socket.addListener(connect, function() { socket.write(Welcome to the TCP Server!n>); });}).listen(4000);
  • 5. Evented I/O benchmarkingAPACHE vs NGINX http://blog.webfaction.com/a-little-holiday-present
  • 6. Evented I/O + V8 Engine libeio: async I/O libev: event loop libuv: wrapper for libev and IOCP There is a single thread running No parallel execution... for YOUR code!db.query().select(*).from(users).execute(function(){ fs.readFile(settings.json, function () { // Block for five seconds :( var now = new Date().getTime(); while(new Date().getTime() < now + 5000); // Response :) });});
  • 7. What about multiple cores? The load balancer approach :1337 :1338 :1339 The OS approachvar http = require(http), cluster = require(cluster);var server = http.createServer(function(req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(TID Rocks!);});cluster(server).listen(1337);
  • 8. And about packages?One line install:# curl http://npmjs.org/install.sh | shNow, let’s install the node.js mysql client?# npm install mysql
  • 9. And about packages?One line install:# curl http://npmjs.org/install.sh | shNow, let’s install the node.js mysql client? There are more than 4900 packages, and more# npm install mysql than 15 are added each day!
  • 10. Let’s check the performance PHP NODE.JS$a = null; var i, a, b, c, max;$b = null;$c = null; max = 1e6;$i = null;$max = 1e6; console.time(maths);$start = microtime(true); for (i = 0; i < max; i++) {for ($i = 0; $i < $max; $i++) a = 1234 + 5678 + i;{ b = 1234 * 5678 + i; $a = 1234 + 5678 + $i; c = 1234 / 2 + i; $b = 1234 * 5678 + $i; } $c = 1234 / 2 + $i;} console.timeEnd(maths);var_dump(microtime(true) - $start); http://www.matt-knight.co.uk/2011/node-js-vs-php-performance-maths/
  • 11. Let’s check the performance RESULTS IN SECONDS PHP NODE.JS$a = null; LOOP SIZE PHP var i, a, b, c, max; NODE.JS$b = null;$c = null; max = 1e6;$i = null;$max = 1e6; console.time(maths); 100.000 0,077 0,002$start = microtime(true); for (i = 0; i < max; i++) {for ($i = 0; $i < $max; $i++) a = 1234 + 5678 + i;{ 1.000.000 0,759 b = 1234 * 5678 + 0,016 i; $a = 1234 + 5678 + $i; c = 1234 / 2 + i; $b = 1234 * 5678 + $i; } $c = 1234 / 2 + $i;} console.timeEnd(maths); 10.000.000 7,605 0,157var_dump(microtime(true) - $start); 100.000.000 75,159 1,567 http://www.matt-knight.co.uk/2011/node-js-vs-php-performance-maths/
  • 12. Who are using node.js?linkedin (m.linkedin.com) - mobile stackThe improvements the team saw were staggering. They went from running 15servers with 15 instances (virtual servers) on each physical machine, to just 4instances that can handle double the traffic.http://venturebeat.com/2011/08/16/linkedin-node/klout (klout.com) - frontend + apiIn our tests, a single node.js process was able to handle thousands ofconcurrent connections in a very CPU-efficient manner. Plus, using JavaScripton both the server and the client made writing multi-purpose code verystraightforward. We knew of other companies using node.js at the time, butmost were using it to serve APIs.http://corp.klout.com/blog/2011/10/the-tech-behind-klout-com/and many others: HP Yahoo!, Nokia, Heroku, VMWare, Loopt .... ,
  • 13. Test Driven Development! TEST RESULT
  • 14. Cross platform socket About Socket.IO Supported languages Socket.IO aims to make realtime apps Cocoa (iOS) - fpotter/socketio-cocoa possible in every browser and mobile Erlang device, blurring the differences - yrashk/socket.io-erlang Flash between the different transport - simb/FlashSocket.IO mechanisms. Its care-free realtime Go - madari/go-socket.io (Currently not compatible with 0.7+) 100% in JavaScript. Java In order to provide realtime connectivity - ibdknox/socket.io-netty - benkay/java-socket.io.client on every browser, Socket.IO selects - Ovea/Socket.IO-Java Lua the most capable transport at runtime, - ignacio/LuaNode-Socket.IO without it affecting the API: MrJoes/tornadio (0.6) - gevent-socketio WebSocket, Adobe® Flash® Socket, Perl AJAX long polling, AJAX multipart - vti/pocketio Python streaming, Forever Iframe and JSONP - MrJoes/tornadio2 (0.7+) Polling. Ruby - markjeee/Socket.IO-rack
  • 15. The node.js ecosystem Express ClusterA Sinatra inspired node.js web development Extensible multi-core server management forframework. nodejs.Jade SupervisorJade is a high performance template engine A little supervisor script for nodejs. It runsheavily influenced by Haml and implemented your program, and watches for codewith JavaScript for node. changes, so you can have hot-codeSocket.io reloading-ish behavior, without worryingAn simple HTTP Socket interface about memory leaks.implementation and server. Joyent (http://no.de)Mongoose Free SmartMachine hosting to deliveryMongoose aims at solving the complexities modern applications with nodejs.associated with asynchronous data storage Expressoby providing a more intuitive API. TDD for nodejs projects in express. Testosterone Another and simple TDD for nodejs.
  • 16. great links:http://nodejs.org/http://no.de/http://howtonode.org/http://jade-lang.com/http://www.commonjs.org/https://github.com/ry/node/wikihttp://en.wikipedia.org/wiki/Event_loophttp://lse.sourceforge.net/io/aio.htmlhttp://code.google.com/p/v8/https://github.com/ry/node/wiki/moduleshttps://github.com/isaacs/npm
  • 17. thanks!http://www.davidruiz.eti.br/ http://www.tid.es/wupsbr@gmail.com @telefonicaid@wupsbr