Your SlideShare is downloading. ×
  • Like
  • Save
Présentation de Node.js
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Présentation de Node.js

  • 5,506 views
Published

Présentation donnée aux conférences SoftShake et RivieraDev 2011.

Présentation donnée aux conférences SoftShake et RivieraDev 2011.

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,506
On SlideShare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • What : (javascript, server, V8) why using Node : (exemples bloquants, non bloquants...)
  • Node.js internal architeture V8 as javascript engine C/C++ bindings
  • Node.js is a single-threaded system. Yes, you heard it right. There is only one thread. Due to its event-loop-based asynchronous and non-blocking nature it scales great even as a single thread, but if you are running on a multi-core CPU you should want to start multiple Node.js processes so you can leverage available cores. Currently, the best solution is to start multiple instances of a node.js app and load-balance them with Nginx.   Node will be able to fork new processes (using the Web Workers API ), but this is something that fits well into the current design. Callbacks after callbacks make it's difficult to see the business process by reading the code. High latency if tasks become blocked waiting for other tasks to complete  
  • Je sortirai bien ca de la prez de 30 min (mais ok pour celle d une heure)

Transcript

  • 1. Node.js     http://nodejs.org
  • 2.
    • Co-founder of Duchess France
    • http://www.java-freelance.fr
    • @MathildeLemee
    • Creator of Web Tambouille
    • http://www.web-tambouille.fr
    • @rmat0n
  • 3. Summary
      • What  ? Why  ?
      • Non blocking API example
      • Event programming model
      • Express, Socket.IO and modules
      • Mini Hands On
      • Unit Test
      • Limits
  • 4. What is Node ?
    • Server-side Javascript
    • Node.js javascript implementation is V8 Javascript Engine (Google Chrome)
    • Provide an easy way to build scalable network programs
    • Non blocking I/O
    • Single Threaded
    • Written by Ryah Dahl
  • 5. What is Node ? http://codingrelic.geekhold.com/2010/08/nodejs-from-30000-feet.html
  • 6. What is Node ?
    • var http = require( 'http' );
    • http.createServer(function (request, response) { 
    •      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 server.js
    • ~$ curl http://127.0.0.1:1337/
  • 7. Why using Node ?
      • Ryan Dahl: "Node.js project: To provide a purely evented, non-blocking infrastructure to script highly concurrent programs" ( http://yuilibrary.com/theater/ryan-dahl/dahl-node/ )
      • Scalable software
      • Non blocking I/O
      • Same language and share code between server side and client side
      • JSON friendly (web, server, database...)
  • 8. Blocking API example
    • print( "hello" );
    • sleep( 2000 );
    • print( "world" );
    blocked!!
  • 9. Non blocking API example
    • var data = File.read( "file.txt" );
    • ...
    • // Here you have to wait... maybe a lot...
    • ...
    • // And your thread is still alive... doing nothing...
    • ...
    • parseResult(data);
  • 10. Non blocking API example
    • var data = File.read( "file.txt" , function(data) {
    •     parseResult(data);
    • });
    • // Here, your thread is alive and continue working !!!
    • myOtherCode();
  • 11.  
  • 12. Event programming model
      • Events are the heart of Node.js
      • Everything is event based
      • You can create yours own events
  • 13. Event programming model Sample
    • dummyEmitter.emit('myCustomEvent', 'myValue');
    • dummyReceiver.on('myCustomEvent', function(data){
    •     console.log(data);
    • });
  • 14. Event programming model Sample
    • socket.emit('news', { hello: 'world' });
    • socket.on('event', function (data) {
    •      console.log(data);
    • });
    SERVEUR CLIENT socket.on('news', function (data) { console.log(data); socket.emit('event', { my: 'data' }); });
  • 15. NPM
  • 16.  
  • 17. Modules
    • Node Boilerplate
    • Event Emitter 2
    • Underscore
    • Vows
    • Coffeemate
    • Node Inspector
    • Spotify, Twitter, Gravatar, Dropbox, AWS, 
    • https://github.com/joyent/node/wiki/modules
  • 18. Express var app = express.createServer(); app.get( '/' , function(req, res) {      res.send( 'Hello World' ); }); app.listen( 3000 ); Web Framework Inspired by Sinatra (Ruby) Systèmes de templates (jade, Haml, EJS...)
  • 19. Express
    • app.configure( 'development' , function() {
    •      server.set( 'views' , __dirname + '/views' );
    •      server.set( 'view engine' , 'ejs' );
    •      app.use(express.static( __dirname + '/public' ));
    •      app.use(express.errorHandler({ dumpExceptions: true ,
    •                                                          showStack: true }));
    • });
    • app.configure( 'production' , function() {
    •      server.set( 'views' , __dirname +  '/views' );
    •      server.set( 'view engine' ,  'ejs' );
    •      var aYear = 31557600000 ;
    •      app.use(express.static( __dirname + '/public' , { 'maxAge' : aYear }));
    •      app.use(express.errorHandler());
    • });
  • 20. Express
    • app.get( '/user/:id' , function(req, res) {
    •      res.send( 'user' + req.params.id);
    • });
    • '/users/:id?'
    •      /users/5 
    •      /users
    • '/user/:id.:format?'
    •      /user/12
    •     /user/12.json
    • '/user/:id/:operation?'
    •      /user/1
    •      /user/1/edit
    • '/files/*'
    •      /files/jquery.js
    •      /files/javascripts/jquery.js
  • 21. Socket.IO - Why ?
    • WebSocket :   Firefox 4, Chrome 4, Opera 10.70, and Safari 5.
    • Asynchronous communication from client to server.
    • AJAX - XmlHttpRequest ?
    •  
    • Flash / AJAX Long Polling
    • Disconnection
  • 22. Socket.IO - How
    • var io = require( 'socket.io' ).listen( 80 );
    • io.sockets.on( 'connection' , function (socket) {
    •      io.sockets.emit( 'info' , 'a player join the game');
    •      socket.on( 'chat' , function (msg) {
    •          console.log( 'send a chat' , msg);
    •      });
    •      socket.on( 'disconnect' , function () {
    •          sockets.emit( 'user disconnected' );
    •      });
    • });
  • 23. Socket.IO - How
    • <script> 
    •     var socket = io.connect( 'http://localhost' );
    •     socket.on( 'connect' , function () {
    •       socket.on( 'info' , function (data) { 
    •           alert(data);
    •       });
    •     });
    •     socket.emit( 'chat' ,myMessage);
    • </script>
  • 24. Socket.IO - How
      • custom messages :      socket.emit('my custom event',{data:xxx});
      • volatile :     socket.volatile.emit('tweet',tweet);
      • acknoledgements
      • broadcast :     socket.broadcast.emit('hello','world');
      • room :     socket.join('justin bieber fans')
      • send a message in a room :     io.sockets.in('my room').emit('hello','world');
      • storing data :     socket.set('name','Mathilde',function () { //callback });
  • 25. Modules
    • Create yours !
    • hello.js
    • var world = function() {
    •      alert( &quot;helloworld&quot; );
    • };
    • exports.world = world;
    • server.js
    • var hello = require( &quot;./hello&quot; )
    • hello.world();
  • 26. Mini Hands On
    • Go Mathilde o/
  • 27. Tests
    •     MLE
      • Unit tests
        • Node.js provides its own assert module  http://nodejs.org/docs/v0.5.6/api/assert.html
        • QUnit (JQuery)  http://docs.jquery.com/Qunit
        • NodeUnit  https://github.com/caolan/nodeunit
        • Expresso  https://github.com/visionmedia/expresso
      • Integration tests
        • Zombie.js  http://zombie.labnotes.org
      •   Behavior Driven Development
        • vowsjs  http://vowsjs.org/
        • jasmine-node  https://github.com/mhevery/jasmine-node
      • https://github.com/joyent/node/wiki/modules#wiki-testing
  • 28. Unit Tests with QUnit
    • <script>
    • $(document)
    •      .ready(
    •          function() {
    •              module(&quot;1. Game&quot;);
    •                  test(
    •                      &quot;Connais la valeur du joueur suivant en fonction du joueur actuel&quot;,
    •                      function() {
    •                              game = new Game();
    •                              game.take(3);
    •                              game.take(1);
    •                              equals(game.otherPlayer(), &quot;O&quot;, &quot;O est le joueur précédent&quot;);
    •                  });
    • ...
    • </script>
    •                 
  • 29. Unit Tests with QUnit
  • 30. Limits
    • - Cryptic error messages
    • node.js:50 throw e; ^ 
    •   Error: ECONNREFUSED, Connection refused at IOWatcher.callback (net:870:22) at node.js:607:9
    •  
    client.on(&quot;error&quot;, function (err) {      console.log(&quot;Error &quot; + err); });   
  • 31. Limits
    • - Cryptic error messages
    •  
    • - Only one thread (it is also an advantage)
    • - Can be difficult to read
    • - Non async lib/third party decrease perfs
    • - IDE, Tooling, Debugger, Profiler
    • - How to choose a good module ?
  • 32. Node REPL
    • ~$ node
    • > 1+2
    •     3
    • > [1, 2, 3].splice(1,2)
    •   [2, 3]
    • > process.pid
    •   2998
    • > ...
  • 33. Links
    • Quick tour :  http://www.slideshare.net/the_undefined/nodejs-a-quick-tour
    • Node + Websockets :  http://www.slideshare.net/the_undefined/nodejs-a-quick-tour
    • Node beginner :  http://nodebeginner.org/
    • The Node Beginner Book :  https://github.com/ManuelKiessling/NodeBeginnerBook
    • Hands on Node.js :  http://nodetuts.com/handson-nodejs-book.html
    • Node.js in Action (été 2012):  http://www.manning.com/cantelon