SlideShare a Scribd company logo
1 of 36
Ricardo Silva

              Degree in Computer Science @ ISTEC
MSc Computation and Medical Instrumentation @ ISEP

                Software Developer @ Shortcut, Lda

                     (blog): http://blog.ricardosilva.pt

                      (email): nodejs@ricardosilva.pt
Event-driven I/O
    server-side
     JavaScript
environment based
   on V8 Engine
JavaScript
(examples)
JavaScript: Array vs Object
                                     Instantiation

• Array
var ar = []; /* ou new Array(); */




• Object
var carColors = {}; /* ou new Object();*/
JavaScript: Array vs Object
                              Add

• Array
var ar = [];
ar[0] = 'January';
Ou
ar.push(‘March’);


• Object
var carColors = {};
carColors['BMW'] = 'Red';
carColors['Audi'] = 'Blue';
JavaScript: Array vs Object
                            Remove

• Array
ar.splice(0,1);




• Object
delete carColors['BMW'];
JavaScript: Array vs Object
                                               Lookup

• Array
var ar = [];
for (var i = 0; i < carColors.length; i++) {
 alert( ar[i] );
};
Ou
var month = ar[0];
• Object
var carColors = {};
for (var i in carColors) {
 alert( carColors[i] );
};
Ou
var colorOfBWM = carColors[‘BMW’];
JavaScript: callback Functions

var 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.”
JavaScript: Events
var 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;
           }
 };
JavaScript: Events

this.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);
  }
Node.js
Node.js: What is?


“   Node.js is a platform built on Chrome's 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.

                                                                   ”
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)
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)
Node.js
     DEMO
Node.js


“ Come on…, server side
JS 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 thousands of concurrent
  connections which are moderately active
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)
Node.js: Non-Blocking I/O

                        Non-Blocking I/O
Blocking:
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
Node.js: Non-Blocking I/O


             Blocking I/O
Read one.txt (20ms)

                        Read two.txt (10ms)

Total duration (30ms)
Node.js: Non-Blocking I/O


        Non-Blocking I/O
Read 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 execution stack
• Forces you to write more efficient code by
  parallelizing your I/O
• Feels pretty much like AJAX in the browser
Node - Modules
        DEMO
Websockets
Websockets: What is?

“The WebSocket specification—developed as part of the HTML5
initiative—introduced the WebSocket JavaScript interface, which
defines a full-duplex single socket connection over which
messages can be sent between client and server. The WebSocket
standard simplifies much of the complexity around bi-
directional web communication and connection management.

WebSocket represents the next evolutionary step in web
communication compared to Comet and Ajax. However, each
technology has its own unique capabilities. Learn how these
technologies vary so you can make the right choice.”
Websockets: What is?

• Persistent connection between browser / server
• The solution of the problem with RT at browser side
var 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();
Websockets: Compatibility
Websockets: Compatibility

•   We can use different transports:
•   HTML5 WebSockets
•   Flash Socket
•   AJAX Long Polling
•   Forever Iframe
Socket.io
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.”
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 sockets
socket.broadcast.emit('event');

//no need to internally buffer this msg
socket.volatile.emit('event');

//broadcast, nut only to people in this room
socket.broadcast.to('room').emit('event');

//broadcast to everyone
io.sockets.emit('event');
io.sockets.send('hello');
Questions?
      (blog): http://blog.ricardosilva.pt

      (email): nodejs@ricardosilva.pt

More Related Content

What's hot

Node js presentation
Node js presentationNode js presentation
Node js presentation
martincabrera
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
Tom Croucher
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Tom Croucher
 
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
Tom Croucher
 

What's hot (20)

Node js presentation
Node js presentationNode js presentation
Node js presentation
 
Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node js
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
 
Building your first Node app with Connect & Express
Building your first Node app with Connect & ExpressBuilding your first Node app with Connect & Express
Building your first Node app with Connect & Express
 
NodeJS
NodeJSNodeJS
NodeJS
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
 
Node ppt
Node pptNode ppt
Node ppt
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
 
Node.js in production
Node.js in productionNode.js in production
Node.js in production
 
Node js beginner
Node js beginnerNode js beginner
Node js beginner
 
Node.js
Node.jsNode.js
Node.js
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
 
Server Side Event Driven Programming
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven Programming
 
All aboard the NodeJS Express
All aboard the NodeJS ExpressAll aboard the NodeJS Express
All aboard the NodeJS Express
 
Nodejs intro
Nodejs introNodejs intro
Nodejs intro
 
Meetup RomaJS - introduzione interattiva a Node.js - Luca Lanziani - Codemoti...
Meetup RomaJS - introduzione interattiva a Node.js - Luca Lanziani - Codemoti...Meetup RomaJS - introduzione interattiva a Node.js - Luca Lanziani - Codemoti...
Meetup RomaJS - introduzione interattiva a Node.js - Luca Lanziani - Codemoti...
 
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
 

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

Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
Tom Croucher
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
Ran Mizrahi
 
JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big Picture
Simon Willison
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.js
guileen
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1
Mohammad Qureshi
 

Similar to Event-driven IO server-side JavaScript environment based on V8 Engine (20)

soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
NodeJS
NodeJSNodeJS
NodeJS
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.
 
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
 
Introducing to node.js
Introducing to node.jsIntroducing to node.js
Introducing to node.js
 
Node azure
Node azureNode azure
Node azure
 
JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big Picture
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.js
 
Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2
 
Why Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiWhy Nodejs Guilin Shanghai
Why Nodejs Guilin Shanghai
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.js
 
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1
 
Comet with node.js and V8
Comet with node.js and V8Comet with node.js and V8
Comet with node.js and V8
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

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

  • 1. Ricardo Silva Degree in Computer Science @ ISTEC MSc Computation and Medical Instrumentation @ ISEP Software Developer @ Shortcut, Lda (blog): http://blog.ricardosilva.pt (email): nodejs@ricardosilva.pt
  • 2. Event-driven I/O server-side JavaScript environment based on V8 Engine
  • 4. JavaScript: Array vs Object Instantiation • Array var ar = []; /* ou new Array(); */ • Object var carColors = {}; /* ou new Object();*/
  • 5. JavaScript: Array vs Object Add • Array var ar = []; ar[0] = 'January'; Ou ar.push(‘March’); • Object var carColors = {}; carColors['BMW'] = 'Red'; carColors['Audi'] = 'Blue';
  • 6. JavaScript: Array vs Object Remove • Array ar.splice(0,1); • Object delete carColors['BMW'];
  • 7. JavaScript: Array vs Object Lookup • Array var ar = []; for (var i = 0; i < carColors.length; i++) { alert( ar[i] ); }; Ou var month = ar[0]; • Object var carColors = {}; for (var i in carColors) { alert( carColors[i] ); }; Ou var colorOfBWM = carColors[‘BMW’];
  • 8. JavaScript: callback Functions var 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. JavaScript: Events var 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. JavaScript: Events this.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); }
  • 12. Node.js: What is? “ Node.js is a platform built on Chrome's 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. 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. 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. Node.js DEMO
  • 16. Node.js “ Come on…, server side JS has been around since 1996 …”
  • 17. Node.js “ …what is so special about node? ”
  • 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. 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. Node.js: Non-Blocking I/O Non-Blocking I/O Blocking: 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. Node.js: Non-Blocking I/O Blocking I/O Read one.txt (20ms) Read two.txt (10ms) Total duration (30ms)
  • 22. Node.js: Non-Blocking I/O Non-Blocking I/O Read one.txt (20ms) Read two.txt (10ms) Total duration (20ms)
  • 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
  • 26. Websockets: What is? “The WebSocket specification—developed as part of the HTML5 initiative—introduced the WebSocket JavaScript interface, which defines a full-duplex single socket connection over which messages can be sent between client and server. The WebSocket standard simplifies much of the complexity around bi- directional web communication and connection management. WebSocket represents the next evolutionary step in web communication compared to Comet and Ajax. However, each technology has its own unique capabilities. Learn how these technologies vary so you can make the right choice.”
  • 27. Websockets: What is? • Persistent connection between browser / server • The solution of the problem with RT at browser side var 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();
  • 29. Websockets: Compatibility • We can use different transports: • HTML5 WebSockets • Flash Socket • AJAX Long Polling • Forever Iframe
  • 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. Socket.io: transports? • Web Socket • Flash Socket • HTML File • XHR Polling • JSONP Polling
  • 33. Socket.io: How to install? npm install socket.io
  • 34. Socket.io DEMO
  • 35. Socket.io: Other methods? //broadcast a message to all sockets socket.broadcast.emit('event'); //no need to internally buffer this msg socket.volatile.emit('event'); //broadcast, nut only to people in this room socket.broadcast.to('room').emit('event'); //broadcast to everyone io.sockets.emit('event'); io.sockets.send('hello');
  • 36. Questions? (blog): http://blog.ricardosilva.pt (email): nodejs@ricardosilva.pt