SlideShare a Scribd company logo
1 of 16
Jugando con
websockets en

   Israel Gutiérrez
NodeJS
• Server side JavaScript
• Platform built over Chrome’s v8
• Event driven (asynchronous)
• Applications
 • Data-intensive (efficiency)
 • Real-time (websockets)
NodeJS architecture
npm

• node package manager
 • express (routing)
 • socket.io (websockets)
 • mongo (database drivers)
 • ...
Application stack
• HTTP server (or TCP-socket or
  websockets)
• Router
• Request handlers
 • Handle data received
 • View logic
HTTP server

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);
TCP server
var net = require('net');

var server = net.createServer(function (socket) {
  socket.write('Echo serverrn');
  socket.pipe(socket);
});

server.listen(1337, '127.0.0.1');
Modules
server.js
var http = require("http");

function start() {
  function onRequest(request, response) {
    console.log("Request received.");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
  }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}

exports.start = start;


index.js
var server = require("./server");

server.start();
Router and handlers
• You can program the routes based on
  request.url
      var server = require("./server");
      var router = require("./router");
      var requestHandlers = require("./requestHandlers");

      var handle = {}
      handle["/"] = requestHandlers.start;
      handle["/start"] = requestHandlers.start;
      handle["/upload"] = requestHandlers.upload;

      server.start(router.route, handle);




• I recommend using express (Sinatra like)
Templating

• ejs
• underscore
• jade
• ...
Websockets
• Canal de comunicación full duplex
  (bidireccional y a la vez) sobre TCP
• No hay cabeceras HTTP! (menos
  overhead)
• Aplicaciones que requieren interacciones
  en tiempo real
• URL tipo ws://
socket.io
• Websockets library
• Fallbacks
 • Flash
 • AJAX long polling
 • AJAX multipart streaming
 • Forever iframe
 • JSONP polling
socket.io support
    Desktop
•   Internet Explorer 5.5+
•   Safari 3+
•   Google Chrome 4+
•   Firefox 3+
•   Opera 10.61+


Mobile
• iPhone Safari
• iPad Safari
• Android WebKit
• WebOs WebKit
socket.io example
Server
var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});



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>
References

• http://nodejs.org/
• http://www.nodebeginner.org/
• https://npmjs.org/
• http://socket.io/
• http://www.nodecloud.org/
Let’s do some fucking
        code!!!

More Related Content

What's hot

Rapid dev env DevOps Warsaw July 2014
Rapid dev env DevOps Warsaw July 2014Rapid dev env DevOps Warsaw July 2014
Rapid dev env DevOps Warsaw July 2014
blndrt
 
(WS14) Sasa Matijasic - Node.js i "novi" web
(WS14) Sasa Matijasic - Node.js i "novi" web(WS14) Sasa Matijasic - Node.js i "novi" web
(WS14) Sasa Matijasic - Node.js i "novi" web
Web::Strategija
 
SockJS Intro
SockJS IntroSockJS Intro
SockJS Intro
Ngoc Dao
 
Helma / RingoJS – Vienna.js Minitalk
Helma / RingoJS – Vienna.js MinitalkHelma / RingoJS – Vienna.js Minitalk
Helma / RingoJS – Vienna.js Minitalk
Philipp Naderer
 
Real-time Web Application with Socket.IO, Node.js, and Redis
Real-time Web Application with Socket.IO, Node.js, and RedisReal-time Web Application with Socket.IO, Node.js, and Redis
Real-time Web Application with Socket.IO, Node.js, and Redis
York Tsai
 

What's hot (19)

Ether mining 101 v2
Ether mining 101 v2Ether mining 101 v2
Ether mining 101 v2
 
Rapid dev env DevOps Warsaw July 2014
Rapid dev env DevOps Warsaw July 2014Rapid dev env DevOps Warsaw July 2014
Rapid dev env DevOps Warsaw July 2014
 
Node.js
Node.jsNode.js
Node.js
 
OSMC 2019 | Monitoring Nomad with Prometheus and Icinga by Bram Vogelaar
OSMC 2019 | Monitoring Nomad with Prometheus and Icinga by Bram VogelaarOSMC 2019 | Monitoring Nomad with Prometheus and Icinga by Bram Vogelaar
OSMC 2019 | Monitoring Nomad with Prometheus and Icinga by Bram Vogelaar
 
Create Rest API in Nodejs
Create Rest API in Nodejs Create Rest API in Nodejs
Create Rest API in Nodejs
 
(WS14) Sasa Matijasic - Node.js i "novi" web
(WS14) Sasa Matijasic - Node.js i "novi" web(WS14) Sasa Matijasic - Node.js i "novi" web
(WS14) Sasa Matijasic - Node.js i "novi" web
 
Socket programming, and openresty
Socket programming, and openrestySocket programming, and openresty
Socket programming, and openresty
 
Frontend Track NodeJS
Frontend Track NodeJSFrontend Track NodeJS
Frontend Track NodeJS
 
Server side scripting smack down - Node.js vs PHP
Server side scripting smack down - Node.js vs PHPServer side scripting smack down - Node.js vs PHP
Server side scripting smack down - Node.js vs PHP
 
vert.x 3.1 - be reactive on the JVM but not only in Java
vert.x 3.1 - be reactive on the JVM but not only in Javavert.x 3.1 - be reactive on the JVM but not only in Java
vert.x 3.1 - be reactive on the JVM but not only in Java
 
SockJS Intro
SockJS IntroSockJS Intro
SockJS Intro
 
WebSocket
WebSocketWebSocket
WebSocket
 
node.js dao
node.js daonode.js dao
node.js dao
 
Helma / RingoJS – Vienna.js Minitalk
Helma / RingoJS – Vienna.js MinitalkHelma / RingoJS – Vienna.js Minitalk
Helma / RingoJS – Vienna.js Minitalk
 
Socket.IO
Socket.IOSocket.IO
Socket.IO
 
Real-time Web Application with Socket.IO, Node.js, and Redis
Real-time Web Application with Socket.IO, Node.js, and RedisReal-time Web Application with Socket.IO, Node.js, and Redis
Real-time Web Application with Socket.IO, Node.js, and Redis
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 
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
 
Complete MVC on NodeJS
Complete MVC on NodeJSComplete MVC on NodeJS
Complete MVC on NodeJS
 

Similar to Jugando con websockets en nodeJS

Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Richard Lee
 
Node.JS and WebSockets with Faye
Node.JS and WebSockets with FayeNode.JS and WebSockets with Faye
Node.JS and WebSockets with Faye
Matjaž Lipuš
 

Similar to Jugando con websockets en nodeJS (20)

Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentation
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Node.js: The What, The How and The When
Node.js: The What, The How and The WhenNode.js: The What, The How and The When
Node.js: The What, The How and The When
 
Get Real: Adventures in realtime web apps
Get Real: Adventures in realtime web appsGet Real: Adventures in realtime web apps
Get Real: Adventures in realtime web apps
 
Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?
 
[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell Folks[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell Folks
 
Node.JS and WebSockets with Faye
Node.JS and WebSockets with FayeNode.JS and WebSockets with Faye
Node.JS and WebSockets with Faye
 
Building Client-Side Attacks with HTML5 Features
Building Client-Side Attacks with HTML5 FeaturesBuilding Client-Side Attacks with HTML5 Features
Building Client-Side Attacks with HTML5 Features
 
Socket.io
Socket.ioSocket.io
Socket.io
 
What is Node.js
What is Node.jsWhat is Node.js
What is Node.js
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2
 
Asynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaAsynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and Java
 
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
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101
 
Node.js - The New, New Hotness
Node.js - The New, New HotnessNode.js - The New, New Hotness
Node.js - The New, New Hotness
 
signalr
signalrsignalr
signalr
 
Introduction to Ethereum
Introduction to EthereumIntroduction to Ethereum
Introduction to Ethereum
 

More from Israel Gutiérrez

Research questions by the Blueberries
Research questions by the BlueberriesResearch questions by the Blueberries
Research questions by the Blueberries
Israel Gutiérrez
 
Management of Assessment Resources in a Federated Repository of Educational R...
Management of Assessment Resources in a Federated Repository of Educational R...Management of Assessment Resources in a Federated Repository of Educational R...
Management of Assessment Resources in a Federated Repository of Educational R...
Israel Gutiérrez
 

More from Israel Gutiérrez (17)

All you need is front
All you need is frontAll you need is front
All you need is front
 
Make startup development great again!
Make startup development great again!Make startup development great again!
Make startup development great again!
 
Emoticritico: midiendo las emociones de los políticos
Emoticritico: midiendo las emociones de los políticosEmoticritico: midiendo las emociones de los políticos
Emoticritico: midiendo las emociones de los políticos
 
Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums
Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums
Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums
 
Learning Analytics Support for Just-in-time Teaching
Learning Analytics Support for Just-in-time TeachingLearning Analytics Support for Just-in-time Teaching
Learning Analytics Support for Just-in-time Teaching
 
Full metal mongo
Full metal mongoFull metal mongo
Full metal mongo
 
Transfórmate en un profesor con superpoderes
Transfórmate en un profesor con superpoderesTransfórmate en un profesor con superpoderes
Transfórmate en un profesor con superpoderes
 
Enhancing orchestration of lab sessions by means of awareness mechanisms
Enhancing orchestration of lab sessions by means of awareness mechanismsEnhancing orchestration of lab sessions by means of awareness mechanisms
Enhancing orchestration of lab sessions by means of awareness mechanisms
 
Pushing the awareness envelope
Pushing the awareness envelopePushing the awareness envelope
Pushing the awareness envelope
 
Stay at KU Leuven
Stay at KU LeuvenStay at KU Leuven
Stay at KU Leuven
 
MongoDB at GUL
MongoDB at GULMongoDB at GUL
MongoDB at GUL
 
Orchestration and Feedback in Lab Sessions: ECTEL11
Orchestration and Feedback in Lab Sessions: ECTEL11Orchestration and Feedback in Lab Sessions: ECTEL11
Orchestration and Feedback in Lab Sessions: ECTEL11
 
The feedback loop revisited
The feedback loop revisitedThe feedback loop revisited
The feedback loop revisited
 
JTELSS11 gradient presentation
JTELSS11 gradient presentationJTELSS11 gradient presentation
JTELSS11 gradient presentation
 
Research questions by the Blueberries
Research questions by the BlueberriesResearch questions by the Blueberries
Research questions by the Blueberries
 
Seminario eMadrid
Seminario eMadridSeminario eMadrid
Seminario eMadrid
 
Management of Assessment Resources in a Federated Repository of Educational R...
Management of Assessment Resources in a Federated Repository of Educational R...Management of Assessment Resources in a Federated Repository of Educational R...
Management of Assessment Resources in a Federated Repository of Educational R...
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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
 
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?
 
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...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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 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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Jugando con websockets en nodeJS

  • 1. Jugando con websockets en Israel Gutiérrez
  • 2. NodeJS • Server side JavaScript • Platform built over Chrome’s v8 • Event driven (asynchronous) • Applications • Data-intensive (efficiency) • Real-time (websockets)
  • 4. npm • node package manager • express (routing) • socket.io (websockets) • mongo (database drivers) • ...
  • 5. Application stack • HTTP server (or TCP-socket or websockets) • Router • Request handlers • Handle data received • View logic
  • 6. HTTP server var http = require("http"); http.createServer(function(request, response) {   response.writeHead(200, {"Content-Type": "text/plain"});   response.write("Hello World");   response.end(); }).listen(8888);
  • 7. TCP server var net = require('net'); var server = net.createServer(function (socket) { socket.write('Echo serverrn'); socket.pipe(socket); }); server.listen(1337, '127.0.0.1');
  • 8. Modules server.js var http = require("http"); function start() {   function onRequest(request, response) {     console.log("Request received.");     response.writeHead(200, {"Content-Type": "text/plain"});     response.write("Hello World");     response.end();   }   http.createServer(onRequest).listen(8888);   console.log("Server has started."); } exports.start = start; index.js var server = require("./server"); server.start();
  • 9. Router and handlers • You can program the routes based on request.url var server = require("./server"); var router = require("./router"); var requestHandlers = require("./requestHandlers"); var handle = {} handle["/"] = requestHandlers.start; handle["/start"] = requestHandlers.start; handle["/upload"] = requestHandlers.upload; server.start(router.route, handle); • I recommend using express (Sinatra like)
  • 11. Websockets • Canal de comunicación full duplex (bidireccional y a la vez) sobre TCP • No hay cabeceras HTTP! (menos overhead) • Aplicaciones que requieren interacciones en tiempo real • URL tipo ws://
  • 12. socket.io • Websockets library • Fallbacks • Flash • AJAX long polling • AJAX multipart streaming • Forever iframe • JSONP polling
  • 13. socket.io support Desktop • Internet Explorer 5.5+ • Safari 3+ • Google Chrome 4+ • Firefox 3+ • Opera 10.61+ Mobile • iPhone Safari • iPad Safari • Android WebKit • WebOs WebKit
  • 14. socket.io example Server var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); 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>
  • 15. References • http://nodejs.org/ • http://www.nodebeginner.org/ • https://npmjs.org/ • http://socket.io/ • http://www.nodecloud.org/
  • 16. Let’s do some fucking code!!!

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