Jugando con websockets en nodeJS

878 views
794 views

Published on

Presentation of websockets usage in nodeJS, in the jsuc3m study group.

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

No Downloads
Views
Total views
878
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
18
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Jugando con websockets en nodeJS

    1. 1. Jugando conwebsockets en Israel Gutiérrez
    2. 2. NodeJS• Server side JavaScript• Platform built over Chrome’s v8• Event driven (asynchronous)• Applications • Data-intensive (efficiency) • Real-time (websockets)
    3. 3. NodeJS architecture
    4. 4. npm• node package manager • express (routing) • socket.io (websockets) • mongo (database drivers) • ...
    5. 5. Application stack• HTTP server (or TCP-socket or websockets)• Router• Request handlers • Handle data received • View logic
    6. 6. HTTP servervar http = require("http");http.createServer(function(request, response) {  response.writeHead(200, {"Content-Type": "text/plain"});  response.write("Hello World");  response.end();}).listen(8888);
    7. 7. TCP servervar net = require(net);var server = net.createServer(function (socket) { socket.write(Echo serverrn); socket.pipe(socket);});server.listen(1337, 127.0.0.1);
    8. 8. Modulesserver.jsvar 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.jsvar server = require("./server");server.start();
    9. 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)
    10. 10. Templating• ejs• underscore• jade• ...
    11. 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. 12. socket.io• Websockets library• Fallbacks • Flash • AJAX long polling • AJAX multipart streaming • Forever iframe • JSONP polling
    13. 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. 14. socket.io exampleServervar 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. 15. References• http://nodejs.org/• http://www.nodebeginner.org/• https://npmjs.org/• http://socket.io/• http://www.nodecloud.org/
    16. 16. Let’s do some fucking code!!!

    ×