SlideShare a Scribd company logo
Server Side Programming
                Using Node.JS


               Ana-Maria Daneliuc
                  Bogdan Gaza


TW2011
$whoarewe


         Students at Faculty of Computer Science


                ana.daneliuc@info.uaic.ro
                bogdan.gaza@info.uaic.ro




TW2011
JavaScript


TW2011
Java is to JavaScript
            Like ham is to hamster




TW2011
JavaScript

         • Language of the Internet
         • Scripting language (interpreted)
         • Client side - usually run by the browser
         • Object oriented
         • Event driven
TW2011
JavaScript

         • Growing in popularity
         • Big companies spend a lot of $$$ to
           improve JavaScript
         • Probably will become a very popular
           language



TW2011
JavaScript
         • Until recent no clear - server side
           JavaScript console interpreter
         • Event driven on the server side also
         • Code sharing
         • All fast evented network servers were
           written in C/C++


TW2011
Node.JS
         Server Side JavaScript




TW2011
Node.js
         • Event driven I/O framework
         • Build upon V8 engine
         • Server Side (includes system calls +
           different modules)
         • Intended for writing scalable network
           programs
         • Written in C/C++ and JavaScript
TW2011
V8
TW2011
V8 JavaScript Engine

         • JavaScript VM used by Google Chrome
         • Written by one of the leading VM engineers
           with 20 years of building VMs - Lars Bak
         • The last VM written by Lars Bak - improved
           JavaScript 20x times.



TW2011
V8 JavaScript Engine

         • JavaScript to assembler
         • Improved garbage collector
         • Independent from Google Chrome
         • Limitations: No bytecode/No threads/No
           processes


TW2011
Non Blocking


TW2011
Blocking




     puts("Enter your name: ");
     var name = gets();
     puts("Name: " + name);




TW2011
Non-Blocking - Node.js way




         puts("Enter your name: ");
         gets(function(name){
             puts("Name: " + name);
         });




TW2011
Remember
                                                     Select
                                                     From the “Retele”



                      Node.js
                                                     Course

                                                     aka explain select
                                                     say about poll/epoll




         When receiving data (from network / disk /
         another source) there must be a callback

                         Data is received
            Data                             Event
           Source                            loop



                                                 CallBack



                                              User
                                            Function

TW2011
Non-blocking vs threaded




TW2011
Non-blocking vs threaded




TW2011
Node.js - Hello World


         var http = require('http');
         http.createServer(function (req, res) {
           res.writeHead(200, {'Content-Type': 'text/plain'});
           res.end('Hello Worldn');
         }).listen(8124, "127.0.0.1");
         console.log('Server running at http://127.0.0.1:8124/');




TW2011
Why use Node.js?


TW2011
Real time web
          applications


TW2011
Ajax

         • Asynchronous JavaScript and XML
         • Method to load data asynchronously
         • Uses XMLHttpRequest object (from JS)
         • Nearly real time

TW2011
Step 1




                  Loading
          Web
                            Page
         Server




TW2011
Step 2




                     Done

          Web
                                 Page
         Server   Ajax Request




TW2011
Step 3




                     Done

          Web
                                 Page
         Server   Ajax Request




TW2011
Step 4




                     Done

          Web
                                 Page
         Server   Ajax Request




TW2011
Step 5




                               Done

          Web
                                                Page
         Server             Ajax Request




                  For another request goto step 1
TW2011
Comet

         Long-lived HTTP connections
         Reduce latency
         Messages are passed from the server to the
         client
         PUSH


TW2011
Step 1




                  Loading
          Web
                            Page
         Server




TW2011
Step 2




                       Done

          Web
                                    Page
         Server   Long lived HTTP




TW2011
Step 3


          Event


                       Done

          Web
                                    Page
         Server   Long lived HTTP




TW2011
Step 4




                       Done

          Web
                                    Page
         Server   Long lived HTTP




TW2011
Step 5




                       Done

          Web
                                    Page
         Server   Long lived HTTP




TW2011
Step 6




                                Done

          Web
                                                Page
         Server            Long lived HTTP




                  For another request goto step 1
TW2011
WebSockets

         • Use TCP sockets from the browser
         • Update web page 40 times / s
         • API is work in progress
         • Not supported by all the modern browsers

TW2011
Ajax vs Comet vs WebSockets
         Ajax      Comet     WebSockets




TW2011
DEMO


TW2011
Q/A

TW2011
Thanks!


TW2011

More Related Content

[TW] Node.js

  • 1. Server Side Programming Using Node.JS Ana-Maria Daneliuc Bogdan Gaza TW2011
  • 2. $whoarewe Students at Faculty of Computer Science ana.daneliuc@info.uaic.ro bogdan.gaza@info.uaic.ro TW2011
  • 4. Java is to JavaScript Like ham is to hamster TW2011
  • 5. JavaScript • Language of the Internet • Scripting language (interpreted) • Client side - usually run by the browser • Object oriented • Event driven TW2011
  • 6. JavaScript • Growing in popularity • Big companies spend a lot of $$$ to improve JavaScript • Probably will become a very popular language TW2011
  • 7. JavaScript • Until recent no clear - server side JavaScript console interpreter • Event driven on the server side also • Code sharing • All fast evented network servers were written in C/C++ TW2011
  • 8. Node.JS Server Side JavaScript TW2011
  • 9. Node.js • Event driven I/O framework • Build upon V8 engine • Server Side (includes system calls + different modules) • Intended for writing scalable network programs • Written in C/C++ and JavaScript TW2011
  • 11. V8 JavaScript Engine • JavaScript VM used by Google Chrome • Written by one of the leading VM engineers with 20 years of building VMs - Lars Bak • The last VM written by Lars Bak - improved JavaScript 20x times. TW2011
  • 12. V8 JavaScript Engine • JavaScript to assembler • Improved garbage collector • Independent from Google Chrome • Limitations: No bytecode/No threads/No processes TW2011
  • 14. Blocking puts("Enter your name: "); var name = gets(); puts("Name: " + name); TW2011
  • 15. Non-Blocking - Node.js way puts("Enter your name: "); gets(function(name){ puts("Name: " + name); }); TW2011
  • 16. Remember Select From the “Retele” Node.js Course aka explain select say about poll/epoll When receiving data (from network / disk / another source) there must be a callback Data is received Data Event Source loop CallBack User Function TW2011
  • 19. Node.js - Hello World var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); TW2011
  • 21. Real time web applications TW2011
  • 22. Ajax • Asynchronous JavaScript and XML • Method to load data asynchronously • Uses XMLHttpRequest object (from JS) • Nearly real time TW2011
  • 23. Step 1 Loading Web Page Server TW2011
  • 24. Step 2 Done Web Page Server Ajax Request TW2011
  • 25. Step 3 Done Web Page Server Ajax Request TW2011
  • 26. Step 4 Done Web Page Server Ajax Request TW2011
  • 27. Step 5 Done Web Page Server Ajax Request For another request goto step 1 TW2011
  • 28. Comet Long-lived HTTP connections Reduce latency Messages are passed from the server to the client PUSH TW2011
  • 29. Step 1 Loading Web Page Server TW2011
  • 30. Step 2 Done Web Page Server Long lived HTTP TW2011
  • 31. Step 3 Event Done Web Page Server Long lived HTTP TW2011
  • 32. Step 4 Done Web Page Server Long lived HTTP TW2011
  • 33. Step 5 Done Web Page Server Long lived HTTP TW2011
  • 34. Step 6 Done Web Page Server Long lived HTTP For another request goto step 1 TW2011
  • 35. WebSockets • Use TCP sockets from the browser • Update web page 40 times / s • API is work in progress • Not supported by all the modern browsers TW2011
  • 36. Ajax vs Comet vs WebSockets Ajax Comet WebSockets TW2011

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n