SlideShare a Scribd company logo
Node.js and
    WebSockets

     Gonzalo Ayuso 2011

          @gonzalo123
http://gonzalo123.wordpress.com
 https://github.com/gonzalo123
Part 1. Node.js
Part 2. WebSockets
Node.js | What's node.js


● JavaScript on the server
● Written by Ryan Dahl
● Based on V8 (Google)

● Asynchronous event-driven model
● Similar in design to:
   ○ Event Machine (Ruby)
   ○ Twisted (Python)
Node.js | Why?

      I/O needs to be done differently
Node.js | Why?

           I/O needs to be done differently
From:
  recordset = db.query("select * from Table");
  render(recordset);
Node.js | Why?

           I/O needs to be done differently



Q: When will you add threads to JavaScript?”
A: over your dead body
                          Brendan Eich (creator of JavaScript)
Node.js | Why?

           I/O needs to be done differently
From:
  recordset = db.query("select * from Table");
  render(recordset);
To:
  db.query("select * from Table", function (recordset) {
      render(recordset)
  });
Node.js | Why?

           I/O needs to be done differently
From:
  recordset = db.query("select * from Table")
  render(recordset)
To:
  db.query("select * from Table", function (recordset) {
      render(recordset)
  });

                         Design Goals

 ● No function should direct perform I/O.
 ● To read info from disk, network, ... there must be a callback
Node.js | Show me the code!

HTTP SERVER
var http = require('http');

http.createServer(function (req, res) {
  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.js | Show me the code!

HTTP SERVER
var http = require('http');
var total = 0;

http.createServer(function (req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  res.end('Hi ' + total + 'n');
  tot++;
}).listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/');
Node.js | Pros and Cons

PROS
 ● Great I/O performance
 ● Just JavaScript. We all know JavaScript
 ● Community
 ● A lot of modules available https://github.
   com/joyent/node/wiki/modules
 ● npm
Node.js | Pros and Cons

PROS
 ● Great I/O performance
 ● Just JavaScript. We all know JavaScript
 ● Community
 ● A lot of modules available https://github.
   com/joyent/node/wiki/modules
 ● npm
CONS
 ● Hosting
 ● We don't really know JavaScript
 ● Modules too young
 ● One thread, one single process for all
 ● Windows support
Part 1. Node.js
Part 2. WebSockets
WebSockets | History

Description of the problem:
 ● Real time communications in Browser
WebSockets | History

Description of the problem:
 ● Real time communications in Browser

Imagine. Let's create simple chat client (5 years ago):

Trivial problem with heavy clients, hard in browsers.
WebSockets | History

COMET (Wikipedia):
Comet is a web application model in which a long-held HTTP request
allows a web server to push data to a browser, without the browser
explicitly requesting it.
WebSockets | History

COMET (Wikipedia):
Comet is a web application model in which a long-held HTTP request
allows a web server to push data to a browser, without the browser
explicitly requesting it.


Problem with COMET:
 ● Servers (keep-alive, MaxClients, ...)
 ● Clients
WebSockets | History

COMET (Wikipedia):
Comet is a web application model in which a long-held HTTP request
allows a web server to push data to a browser, without the browser
explicitly requesting it.


Problem with COMET:
 ● Servers (keep-alive, MaxClients, ...)
 ● Clients
WebSockets | Short Polling / Long
Polling
Short Polling
 ● <meta http-equiv="refresh" content="5">
 ● setInterval and setTimeout
 ● Inneficient
 ● Uses many resources (DB Connection, ...)
WebSockets | Short Polling / Long
Polling
Short Polling
 ● <meta http-equiv="refresh" content="5">
 ● setInterval and setTimeout
 ● Inneficient
 ● Uses many resources (DB Connection, ...)

Long Pooling
 ● Looks like Real Time but it isn't RT
WebSockets | Short Polling / Long
Polling
Short Polling
 ● <meta http-equiv="refresh" content="5">
 ● setInterval and setTimeout
 ● Inneficient
 ● Uses many resources (DB Connection, ...)

Long Pooling
 ● Looks like Real Time but it isn't RT

It works? Yes
It scales? No (Long Polling better but still No)
Is your sysadmin happy? No
WebSockets | Short Polling / Long
Polling
Short Polling
 ● <meta http-equiv="refresh" content="5">
 ● setInterval and setTimeout
 ● Inneficient
 ● Uses many resources (DB Connection, ...)

Long Pooling
 ● Looks like Real Time but it isn't RT

It works? Yes
It scales? No (Long Polling better but still No)
Is your sysadmin happy? No
WebSockets | HTML5

WebSockets
This specification defines an API that enables Web pages to use the
WebSocket protocol for two-way communication with a remote host.
WebSockets | HTML5

WebSockets
This specification defines an API that enables Web pages to use the
WebSocket protocol for two-way communication with a remote host.

That's means:
 ● The solution of the problem with RT at browser side
WebSockets | HTML5

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');
// later...
ws.close();
WebSockets | HTML5




            Cool but ...
WebSockets | HTML5




             Cool but ...
      Not all browsers support it
WebSockets | socket.io

Is there a solution?
WebSockets | socket.io

Is there a solution?

                       http://socket.io/
WebSockets | socket.io

Is there a solution?

                                               http://socket.io/

  Client
<script src="/socket.io/socket.io.js"></script>
<script>
 var socket = io.connect('http://localhost');
 socket.on('news', function (data) {
   console.log(data);
   socket.emit('my other event', { my: 'data' });
 });
</script>
WebSockets | socket.io

Is there a solution?

                                               http://socket.io/

  Client
<script src="/socket.io/socket.io.js"></script>
<script>
 var socket = io.connect('http://localhost');            Server (node.js application)
 socket.on('news', function (data) {
   console.log(data);                                    var io = require('socket.io').listen(80);
   socket.emit('my other event', { my: 'data' });
 });                                                     io.sockets.on('connection', function (socket) {
</script>                                                  socket.emit('news', { hello: 'world' });
                                                           socket.on('my other event', function (data) {
                                                             console.log(data);
                                                           });
                                                         });
WebSockets | socket.io


                       http://socket.io/
Supported transports
 ● WebSocket
 ● Adobe® Flash® Socket
 ● AJAX long polling
 ● AJAX multipart streaming
 ● Forever Iframe
 ● JSONP Polling
WebSockets | socket.io


                       http://socket.io/
Supported transports          Supported browsers
 ● WebSocket                   ● Internet Explorer 5.5+
 ● Adobe® Flash® Socket        ● Safari 3+
 ● AJAX long polling           ● Google Chrome 4+
 ● AJAX multipart streaming    ● Firefox 3+
 ● Forever Iframe              ● Opera 10.61+
 ● JSONP Polling               ● iPhone Safari
                               ● iPad Safari
                               ● Android WebKit
                               ● WebOs WebKit
References


● http://dev.w3.org/html5/websockets/
● https://github.com/joyent/node/wiki/modules
● http://nodejs.org/
● http://socket.io/
● http://en.wikipedia.org/wiki/Push_technology
● http://www.youtube.com/watch?v=jo_B4LTHi3I
● http://gonzalo123.wordpress.
  com/category/technology/node-js/
The End.

    Many thanks
          @gonzalo123
http://gonzalo123.wordpress.com

More Related Content

What's hot (20)

PPTX
What is Node.js
mohamed hadrich
 
PPTX
Node js introduction
Joseph de Castelnau
 
PPTX
Google Chromebook for the Enterprise: Yeah or Meh?
Ericom Software
 
PDF
Kettunen, miaubiz fuzzing at scale and in style
DefconRussia
 
PDF
Real time coding with jWebSocket
Victor Antonio Barzana
 
PDF
Socket.IO
Davide Pedranz
 
PDF
Introduction to REST API with Node.js
Yoann Gotthilf
 
PPTX
NodeJS
Alok Guha
 
PDF
Introduction to Node.js
Jack Franklin
 
PPTX
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
Ericom Software
 
PDF
(C)NodeJS
Jackson Tian
 
PPTX
My month with Ruby
alextomovski
 
PDF
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
Mario Heiderich
 
ODP
Node Js Websocket Js Meetup Slides
Makoto Inoue
 
PDF
Fundamental of Node.JS - Internship Presentation - Week7
Devang Garach
 
PDF
Create a RESTful API with NodeJS, Express and MongoDB
Hengki Sihombing
 
PDF
Require js + backbone, bower and grunt
Joe Fleming
 
PDF
Nodejs presentation
Arvind Devaraj
 
PDF
Socket.io
Diego Pacheco
 
PDF
NodeJS
Predhin Sapru
 
What is Node.js
mohamed hadrich
 
Node js introduction
Joseph de Castelnau
 
Google Chromebook for the Enterprise: Yeah or Meh?
Ericom Software
 
Kettunen, miaubiz fuzzing at scale and in style
DefconRussia
 
Real time coding with jWebSocket
Victor Antonio Barzana
 
Socket.IO
Davide Pedranz
 
Introduction to REST API with Node.js
Yoann Gotthilf
 
NodeJS
Alok Guha
 
Introduction to Node.js
Jack Franklin
 
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
Ericom Software
 
(C)NodeJS
Jackson Tian
 
My month with Ruby
alextomovski
 
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
Mario Heiderich
 
Node Js Websocket Js Meetup Slides
Makoto Inoue
 
Fundamental of Node.JS - Internship Presentation - Week7
Devang Garach
 
Create a RESTful API with NodeJS, Express and MongoDB
Hengki Sihombing
 
Require js + backbone, bower and grunt
Joe Fleming
 
Nodejs presentation
Arvind Devaraj
 
Socket.io
Diego Pacheco
 

Viewers also liked (20)

PDF
Get Soaked - An In Depth Look At PHP Streams
Davey Shafik
 
PPTX
Electrify your code with PHP Generators
Mark Baker
 
PDF
Techniques d'accélération des pages web
Jean-Pierre Vincent
 
ODP
Elastic Searching With PHP
Lea Hänsenberger
 
PDF
Diving deep into twig
Matthias Noback
 
ODP
PHP5.5 is Here
julien pauli
 
PDF
Automation using-phing
Rajat Pandit
 
PDF
The quest for global design principles (SymfonyLive Berlin 2015)
Matthias Noback
 
PDF
Top tips my_sql_performance
afup Paris
 
PDF
Mocking Demystified
Marcello Duarte
 
PDF
Why elasticsearch rocks!
tlrx
 
PDF
Understanding Craftsmanship SwanseaCon2015
Marcello Duarte
 
PDF
Si le tdd est mort alors pratiquons une autopsie mix-it 2015
Bruno Boucard
 
PDF
Writing infinite scalability web applications with PHP and PostgreSQL
Gabriele Bartolini
 
PDF
L'ABC du BDD (Behavior Driven Development)
Arnauld Loyer
 
PDF
Caching on the Edge
Fabien Potencier
 
PDF
TDD with PhpSpec - Lone Star PHP 2016
CiaranMcNulty
 
PDF
Performance serveur et apache
afup Paris
 
PDF
Behat 3.0 meetup (March)
Konstantin Kudryashov
 
PDF
The Wonderful World of Symfony Components
Ryan Weaver
 
Get Soaked - An In Depth Look At PHP Streams
Davey Shafik
 
Electrify your code with PHP Generators
Mark Baker
 
Techniques d'accélération des pages web
Jean-Pierre Vincent
 
Elastic Searching With PHP
Lea Hänsenberger
 
Diving deep into twig
Matthias Noback
 
PHP5.5 is Here
julien pauli
 
Automation using-phing
Rajat Pandit
 
The quest for global design principles (SymfonyLive Berlin 2015)
Matthias Noback
 
Top tips my_sql_performance
afup Paris
 
Mocking Demystified
Marcello Duarte
 
Why elasticsearch rocks!
tlrx
 
Understanding Craftsmanship SwanseaCon2015
Marcello Duarte
 
Si le tdd est mort alors pratiquons une autopsie mix-it 2015
Bruno Boucard
 
Writing infinite scalability web applications with PHP and PostgreSQL
Gabriele Bartolini
 
L'ABC du BDD (Behavior Driven Development)
Arnauld Loyer
 
Caching on the Edge
Fabien Potencier
 
TDD with PhpSpec - Lone Star PHP 2016
CiaranMcNulty
 
Performance serveur et apache
afup Paris
 
Behat 3.0 meetup (March)
Konstantin Kudryashov
 
The Wonderful World of Symfony Components
Ryan Weaver
 
Ad

Similar to Nodejs and WebSockets (20)

PDF
soft-shake.ch - Hands on Node.js
soft-shake.ch
 
PPTX
Event-driven IO server-side JavaScript environment based on V8 Engine
Ricardo Silva
 
ODP
SockJS Intro
Ngoc Dao
 
PDF
Original slides from Ryan Dahl's NodeJs intro talk
Aarti Parikh
 
PPTX
Node.js: The What, The How and The When
FITC
 
PDF
5.node js
Geunhyung Kim
 
KEY
Jugando con websockets en nodeJS
Israel Gutiérrez
 
PPTX
Introduction to Vert.x
Yiguang Hu
 
PDF
Node azure
Emanuele DelBono
 
KEY
Writing robust Node.js applications
Tom Croucher
 
PDF
KSDG-iSlide App 開發心得分享
Chia Wei Tsai
 
KEY
The HTML5 WebSocket API
David Lindkvist
 
PDF
Introduction to Node.js
Somkiat Puisungnoen
 
KEY
Practical Use of MongoDB for Node.js
async_io
 
PDF
Nodejs a-practical-introduction-oredev
Felix Geisendörfer
 
PDF
Cape Cod Web Technology Meetup - 2
Asher Martin
 
PPTX
introduction to node.js
orkaplan
 
KEY
Node.js - A practical introduction (v2)
Felix Geisendörfer
 
ODP
Groovy & Grails eXchange 2012 vert.x presentation
Stuart (Pid) Williams
 
PPTX
Java script at backend nodejs
Amit Thakkar
 
soft-shake.ch - Hands on Node.js
soft-shake.ch
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Ricardo Silva
 
SockJS Intro
Ngoc Dao
 
Original slides from Ryan Dahl's NodeJs intro talk
Aarti Parikh
 
Node.js: The What, The How and The When
FITC
 
5.node js
Geunhyung Kim
 
Jugando con websockets en nodeJS
Israel Gutiérrez
 
Introduction to Vert.x
Yiguang Hu
 
Node azure
Emanuele DelBono
 
Writing robust Node.js applications
Tom Croucher
 
KSDG-iSlide App 開發心得分享
Chia Wei Tsai
 
The HTML5 WebSocket API
David Lindkvist
 
Introduction to Node.js
Somkiat Puisungnoen
 
Practical Use of MongoDB for Node.js
async_io
 
Nodejs a-practical-introduction-oredev
Felix Geisendörfer
 
Cape Cod Web Technology Meetup - 2
Asher Martin
 
introduction to node.js
orkaplan
 
Node.js - A practical introduction (v2)
Felix Geisendörfer
 
Groovy & Grails eXchange 2012 vert.x presentation
Stuart (Pid) Williams
 
Java script at backend nodejs
Amit Thakkar
 
Ad

Recently uploaded (20)

PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
July Patch Tuesday
Ivanti
 
Python basic programing language for automation
DanialHabibi2
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 

Nodejs and WebSockets

  • 1. Node.js and WebSockets Gonzalo Ayuso 2011 @gonzalo123 http://gonzalo123.wordpress.com https://github.com/gonzalo123
  • 2. Part 1. Node.js Part 2. WebSockets
  • 3. Node.js | What's node.js ● JavaScript on the server ● Written by Ryan Dahl ● Based on V8 (Google) ● Asynchronous event-driven model ● Similar in design to: ○ Event Machine (Ruby) ○ Twisted (Python)
  • 4. Node.js | Why? I/O needs to be done differently
  • 5. Node.js | Why? I/O needs to be done differently From: recordset = db.query("select * from Table"); render(recordset);
  • 6. Node.js | Why? I/O needs to be done differently Q: When will you add threads to JavaScript?” A: over your dead body Brendan Eich (creator of JavaScript)
  • 7. Node.js | Why? I/O needs to be done differently From: recordset = db.query("select * from Table"); render(recordset); To: db.query("select * from Table", function (recordset) { render(recordset) });
  • 8. Node.js | Why? I/O needs to be done differently From: recordset = db.query("select * from Table") render(recordset) To: db.query("select * from Table", function (recordset) { render(recordset) }); Design Goals ● No function should direct perform I/O. ● To read info from disk, network, ... there must be a callback
  • 9. Node.js | Show me the code! HTTP SERVER var http = require('http'); http.createServer(function (req, res) { 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/');
  • 10. Node.js | Show me the code! HTTP SERVER var http = require('http'); var total = 0; http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hi ' + total + 'n'); tot++; }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/');
  • 11. Node.js | Pros and Cons PROS ● Great I/O performance ● Just JavaScript. We all know JavaScript ● Community ● A lot of modules available https://github. com/joyent/node/wiki/modules ● npm
  • 12. Node.js | Pros and Cons PROS ● Great I/O performance ● Just JavaScript. We all know JavaScript ● Community ● A lot of modules available https://github. com/joyent/node/wiki/modules ● npm CONS ● Hosting ● We don't really know JavaScript ● Modules too young ● One thread, one single process for all ● Windows support
  • 13. Part 1. Node.js Part 2. WebSockets
  • 14. WebSockets | History Description of the problem: ● Real time communications in Browser
  • 15. WebSockets | History Description of the problem: ● Real time communications in Browser Imagine. Let's create simple chat client (5 years ago): Trivial problem with heavy clients, hard in browsers.
  • 16. WebSockets | History COMET (Wikipedia): Comet is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it.
  • 17. WebSockets | History COMET (Wikipedia): Comet is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Problem with COMET: ● Servers (keep-alive, MaxClients, ...) ● Clients
  • 18. WebSockets | History COMET (Wikipedia): Comet is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Problem with COMET: ● Servers (keep-alive, MaxClients, ...) ● Clients
  • 19. WebSockets | Short Polling / Long Polling Short Polling ● <meta http-equiv="refresh" content="5"> ● setInterval and setTimeout ● Inneficient ● Uses many resources (DB Connection, ...)
  • 20. WebSockets | Short Polling / Long Polling Short Polling ● <meta http-equiv="refresh" content="5"> ● setInterval and setTimeout ● Inneficient ● Uses many resources (DB Connection, ...) Long Pooling ● Looks like Real Time but it isn't RT
  • 21. WebSockets | Short Polling / Long Polling Short Polling ● <meta http-equiv="refresh" content="5"> ● setInterval and setTimeout ● Inneficient ● Uses many resources (DB Connection, ...) Long Pooling ● Looks like Real Time but it isn't RT It works? Yes It scales? No (Long Polling better but still No) Is your sysadmin happy? No
  • 22. WebSockets | Short Polling / Long Polling Short Polling ● <meta http-equiv="refresh" content="5"> ● setInterval and setTimeout ● Inneficient ● Uses many resources (DB Connection, ...) Long Pooling ● Looks like Real Time but it isn't RT It works? Yes It scales? No (Long Polling better but still No) Is your sysadmin happy? No
  • 23. WebSockets | HTML5 WebSockets This specification defines an API that enables Web pages to use the WebSocket protocol for two-way communication with a remote host.
  • 24. WebSockets | HTML5 WebSockets This specification defines an API that enables Web pages to use the WebSocket protocol for two-way communication with a remote host. That's means: ● The solution of the problem with RT at browser side
  • 25. WebSockets | HTML5 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'); // later... ws.close();
  • 26. WebSockets | HTML5 Cool but ...
  • 27. WebSockets | HTML5 Cool but ... Not all browsers support it
  • 28. WebSockets | socket.io Is there a solution?
  • 29. WebSockets | socket.io Is there a solution? http://socket.io/
  • 30. WebSockets | socket.io Is there a solution? http://socket.io/ Client <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script>
  • 31. WebSockets | socket.io Is there a solution? http://socket.io/ Client <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); Server (node.js application) socket.on('news', function (data) { console.log(data); var io = require('socket.io').listen(80); socket.emit('my other event', { my: 'data' }); }); io.sockets.on('connection', function (socket) { </script> socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });
  • 32. WebSockets | socket.io http://socket.io/ Supported transports ● WebSocket ● Adobe® Flash® Socket ● AJAX long polling ● AJAX multipart streaming ● Forever Iframe ● JSONP Polling
  • 33. WebSockets | socket.io http://socket.io/ Supported transports Supported browsers ● WebSocket ● Internet Explorer 5.5+ ● Adobe® Flash® Socket ● Safari 3+ ● AJAX long polling ● Google Chrome 4+ ● AJAX multipart streaming ● Firefox 3+ ● Forever Iframe ● Opera 10.61+ ● JSONP Polling ● iPhone Safari ● iPad Safari ● Android WebKit ● WebOs WebKit
  • 34. References ● http://dev.w3.org/html5/websockets/ ● https://github.com/joyent/node/wiki/modules ● http://nodejs.org/ ● http://socket.io/ ● http://en.wikipedia.org/wiki/Push_technology ● http://www.youtube.com/watch?v=jo_B4LTHi3I ● http://gonzalo123.wordpress. com/category/technology/node-js/
  • 35. The End. Many thanks @gonzalo123 http://gonzalo123.wordpress.com