0
Ricardo Silva              Degree in Computer Science @ ISTECMSc Computation and Medical Instrumentation @ ISEP           ...
Event-driven I/O    server-side     JavaScriptenvironment based   on V8 Engine
JavaScript(examples)
JavaScript: Array vs Object                                     Instantiation• Arrayvar ar = []; /* ou new Array(); */• Ob...
JavaScript: Array vs Object                              Add• Arrayvar ar = [];ar[0] = January;Ouar.push(‘March’);• Object...
JavaScript: Array vs Object                            Remove• Arrayar.splice(0,1);• Objectdelete carColors[BMW];
JavaScript: Array vs Object                                               Lookup• Arrayvar ar = [];for (var i = 0; i < car...
JavaScript: callback Functionsvar state;function setstate (pbool, difCallBack) {         if (state != pbool)              ...
JavaScript: Eventsvar eventHandlers = new Object(); this.registerEvent=function(name,callback){    var eh=(eventHandlers[n...
JavaScript: Eventsthis.triggerEvent=function(name,args){     var eh=eventHandlers[name];     if("object"==typeof eh) for(v...
Node.js
Node.js: What is?“   Node.js is a platform built on Chromes JavaScript runtime for    easily building fast, scalable netwo...
Node.js: Companies using node!•   LinkedIn(Mobile WebApp)      • Yahoo!Mail•   Ebay(Data retrieval gateway) • Rackspace(Cl...
Node.js: How to install?•   $ git clone  git://github.com/joyent/node.git•   $ cd node•   $ git checkout v0.6.0•   $ ./con...
Node.js     DEMO
Node.js“ Come on…, server sideJS has been around since        1996 …”
Node.js“ …what is so special   about node? ”
Node.js: Seep                   Speed• Node can do ~6000 http requests / sec per CPU core• It is no problem to handle thou...
Node.js: V8 JS Engine    V8 JavaScript Engine• Developed by Google in Aarhus, Denmark for Google  Chrome• Translates JavaS...
Node.js: Non-Blocking I/O                        Non-Blocking I/OBlocking:var fs = require(fs);var one = fs.readFileSync(o...
Node.js: Non-Blocking I/O             Blocking I/ORead one.txt (20ms)                        Read two.txt (10ms)Total dura...
Node.js: Non-Blocking I/O        Non-Blocking I/ORead one.txt (20ms)Read two.txt (10ms)Total duration (20ms)
Node.js: Non-Blocking I/O         Non-Blocking I/O• Close to ideal for high concurrency / high through  put, single execut...
Node - Modules        DEMO
Websockets
Websockets: What is?“The WebSocket specification—developed as part of the HTML5initiative—introduced the WebSocket JavaScr...
Websockets: What is?• Persistent connection between browser / server• The solution of the problem with RT at browser sidev...
Websockets: Compatibility
Websockets: Compatibility•   We can use different transports:•   HTML5 WebSockets•   Flash Socket•   AJAX Long Polling•   ...
Socket.io
Socket.io“ Socket.io aims to make realtime apps possible in   every browser and mobile device, blurring the    differences...
Socket.io: transports?•   Web Socket•   Flash Socket•   HTML File•   XHR Polling•   JSONP Polling
Socket.io: How to install?  npm install socket.io
Socket.io      DEMO
Socket.io: Other methods?//broadcast a message to all socketssocket.broadcast.emit(event);//no need to internally buffer t...
Questions?      (blog): http://blog.ricardosilva.pt      (email): nodejs@ricardosilva.pt
Upcoming SlideShare
Loading in...5
×

Event-driven IO server-side JavaScript environment based on V8 Engine

4,268

Published on

Event-driven IO server-side JavaScript environment based on V8 Engine

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

No Downloads
Views
Total Views
4,268
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
81
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Event-driven IO server-side JavaScript environment based on V8 Engine"

  1. 1. Ricardo Silva Degree in Computer Science @ ISTECMSc Computation and Medical Instrumentation @ ISEP Software Developer @ Shortcut, Lda (blog): http://blog.ricardosilva.pt (email): nodejs@ricardosilva.pt
  2. 2. Event-driven I/O server-side JavaScriptenvironment based on V8 Engine
  3. 3. JavaScript(examples)
  4. 4. JavaScript: Array vs Object Instantiation• Arrayvar ar = []; /* ou new Array(); */• Objectvar carColors = {}; /* ou new Object();*/
  5. 5. JavaScript: Array vs Object Add• Arrayvar ar = [];ar[0] = January;Ouar.push(‘March’);• Objectvar carColors = {};carColors[BMW] = Red;carColors[Audi] = Blue;
  6. 6. JavaScript: Array vs Object Remove• Arrayar.splice(0,1);• Objectdelete carColors[BMW];
  7. 7. JavaScript: Array vs Object Lookup• Arrayvar ar = [];for (var i = 0; i < carColors.length; i++) { alert( ar[i] );};Ouvar month = ar[0];• Objectvar carColors = {};for (var i in carColors) { alert( carColors[i] );};Ouvar colorOfBWM = carColors[‘BMW’];
  8. 8. JavaScript: callback Functionsvar state;function setstate (pbool, difCallBack) { if (state != pbool) return (state = pbool), difCallBack(state);}setstate(true, function(s){ alert(s);}); “We can keep on doing other things while waiting for the callback to be called.”
  9. 9. JavaScript: Eventsvar eventHandlers = new Object(); this.registerEvent=function(name,callback){ var eh=(eventHandlers[name])?eventHandlers[name]:(eventHandlers[name]=[]); eh[eh.length]=callback; }; this.unregisterEvent=function(name,callback){ var eh=eventHandlers[name]; if("object"==typeof eh) for(var h in eh) if(eh[h]===callback){ delete eh[h]; return; } };
  10. 10. JavaScript: Eventsthis.triggerEvent=function(name,args){ var eh=eventHandlers[name]; if("object"==typeof eh) for(var h in eh) eh[h](args); }; this.ring=function(evDelegate){ xpto.registerEvent(ring,evDelegate); }; function onRing(args){ xpto.triggerEvent(ring,args); }
  11. 11. Node.js
  12. 12. Node.js: What is?“ Node.js is a platform built on Chromes JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. ”
  13. 13. Node.js: Companies using node!• LinkedIn(Mobile WebApp) • Yahoo!Mail• Ebay(Data retrieval gateway) • Rackspace(Cloud kick monitoring)• GitHub(for Downloads) • Voxxer(Push to Talk mobile app)• Palm/HP(in WebOS)
  14. 14. Node.js: How to install?• $ git clone git://github.com/joyent/node.git• $ cd node• $ git checkout v0.6.0• $ ./configure• $ sudo make install (windows users: download node.exe)
  15. 15. Node.js DEMO
  16. 16. Node.js“ Come on…, server sideJS has been around since 1996 …”
  17. 17. Node.js“ …what is so special about node? ”
  18. 18. Node.js: Seep Speed• Node can do ~6000 http requests / sec per CPU core• It is no problem to handle thousands of concurrent connections which are moderately active
  19. 19. Node.js: V8 JS Engine V8 JavaScript Engine• Developed by Google in Aarhus, Denmark for Google Chrome• Translates JavaScript in Assembly Code• Crankshaft JIT (enabled in 0.6.0 by default)
  20. 20. Node.js: Non-Blocking I/O Non-Blocking I/OBlocking:var fs = require(fs);var one = fs.readFileSync(one.txt,utf-8);console.log(Read file one);var two = fs.readFileSync(two.txt,utf-8);console.log(Read file two);Non-Blocking:var fs = require(fs);fs.readFile(one.txt,utf-8,function(err,data) { console.log(Read file one);});fs.readFile(two.txt,utf-8,function(err,data) { console.log(Read file two);}); DEMO
  21. 21. Node.js: Non-Blocking I/O Blocking I/ORead one.txt (20ms) Read two.txt (10ms)Total duration (30ms)
  22. 22. Node.js: Non-Blocking I/O Non-Blocking I/ORead one.txt (20ms)Read two.txt (10ms)Total duration (20ms)
  23. 23. Node.js: Non-Blocking I/O Non-Blocking I/O• Close to ideal for high concurrency / high through put, single execution stack• Forces you to write more efficient code by parallelizing your I/O• Feels pretty much like AJAX in the browser
  24. 24. Node - Modules DEMO
  25. 25. Websockets
  26. 26. Websockets: What is?“The WebSocket specification—developed as part of the HTML5initiative—introduced the WebSocket JavaScript interface, whichdefines a full-duplex single socket connection over whichmessages can be sent between client and server. The WebSocketstandard simplifies much of the complexity around bi-directional web communication and connection management.WebSocket represents the next evolutionary step in webcommunication compared to Comet and Ajax. However, eachtechnology has its own unique capabilities. Learn how thesetechnologies vary so you can make the right choice.”
  27. 27. Websockets: What is?• Persistent connection between browser / server• The solution of the problem with RT at browser sidevar ws = new WebSocket(url);ws.onopen = function() { // When the connection opens};ws.onmessage = function() { // When the server sends data};ws.onclose = function() { // When the connection is closed};ws.send (Hi all!);ws.close();
  28. 28. Websockets: Compatibility
  29. 29. Websockets: Compatibility• We can use different transports:• HTML5 WebSockets• Flash Socket• AJAX Long Polling• Forever Iframe
  30. 30. Socket.io
  31. 31. Socket.io“ Socket.io aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanism. It’s care-free realtime 100% in JavaScript.”
  32. 32. Socket.io: transports?• Web Socket• Flash Socket• HTML File• XHR Polling• JSONP Polling
  33. 33. Socket.io: How to install? npm install socket.io
  34. 34. Socket.io DEMO
  35. 35. Socket.io: Other methods?//broadcast a message to all socketssocket.broadcast.emit(event);//no need to internally buffer this msgsocket.volatile.emit(event);//broadcast, nut only to people in this roomsocket.broadcast.to(room).emit(event);//broadcast to everyoneio.sockets.emit(event);io.sockets.send(hello);
  36. 36. Questions? (blog): http://blog.ricardosilva.pt (email): nodejs@ricardosilva.pt
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×