Your SlideShare is downloading. ×
0
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Node.js and Web Sockets
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Node.js and Web Sockets

2,118

Published on

A presentation give to @CSLU1 about Node.js and Web sockets for real time communication.

A presentation give to @CSLU1 about Node.js and Web sockets for real time communication.

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

No Downloads
Views
Total Views
2,118
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Node.js & WebSockets EventBased I/O Web Programming @danjamkerTuesday, 5 February 13
  • 2. What is Node.jsTuesday, 5 February 13
  • 3. Web Server TCP Server Music Machine Command line application Proxy Server Voice Mail ServerTuesday, 5 February 13
  • 4. Node.js is not another web frame workTuesday, 5 February 13
  • 5. It is a ServerTuesday, 5 February 13
  • 6. Basic HTTP Server var http = require(‘http’); var server = http.createServer(function(req, res){ res.writeHead(200); res.end(“Hello World”); }); server.listed(400);Tuesday, 5 February 13
  • 7. Server Side Java Script Using Google V8 Engine (Very Fast)Tuesday, 5 February 13
  • 8. Non Blocking I/O Multi Thread I/O Loop / Event BasedTuesday, 5 February 13
  • 9. Asynchronous Non Blocking By DefaultTuesday, 5 February 13
  • 10. Why Non BlockingTuesday, 5 February 13
  • 11. Tuesday, 5 February 13
  • 12. Tuesday, 5 February 13
  • 13. Blocking I/O $query = “SELECT * FROM users WHERE id = ?”; $users = query($query, array($id)); print_r($users); $query = “SELECT * FROM activities ORDER BY timestamp LIMIT 50”; $activities = query($query); print_r($activities); $query = “SELECT count(points) as total, user_id FROM activities LIMIT 50”; $leader_board = query($query); 270s Total Time to completeTuesday, 5 February 13
  • 14. Non-Blocking I/OTuesday, 5 February 13
  • 15. Callbacks A callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback or it might happen at later time, as in an asynchronous callback.Tuesday, 5 February 13
  • 16. NPM Node Packet ManagerTuesday, 5 February 13
  • 17. NPM Install npm install <module> Search npm search <string> List all models on computer npm list module info npm info <module name>Tuesday, 5 February 13
  • 18. Popular NPM Modules Express (Web frame work) socket.io (Web scoket JS frame work MORE LATER) redis (Cominication to redis DB) Async.js (straight-forward, asynchronous JavaScript)Tuesday, 5 February 13
  • 19. Pros Cons Great I/O performance Hosting We all know JS We don’t really know JS Community Young Modules Modules / NPM One Thread Windows SupportTuesday, 5 February 13
  • 20. HostingTuesday, 5 February 13
  • 21. HTML 5Tuesday, 5 February 13
  • 22. WebSocketsTuesday, 5 February 13
  • 23. What are web sockets W3C API and IETF Protocol Full-duplex, single socket Enables pages to communicate with remote host Traverses firewalls, proxies, and routers Cross-Origin Resource SharingTuesday, 5 February 13
  • 24. Web Socket Traffic HTTP connection upgraded to WS using TCP Each frame of data: Starts with 0x00 byte and ends with 0xFF Contains UFT-8 data in-between No defined data limit. JS does not allow more than 4GBTuesday, 5 February 13
  • 25. LOW OVERHEADSTuesday, 5 February 13
  • 26. AJAX (Asynchronous Javascript + XML) Content can change whiteout page loading User-Perceived low capacity Normal HTTP headersTuesday, 5 February 13
  • 27. LARGE OVERHEADSTuesday, 5 February 13
  • 28. AJAX Polling Polling Web Socket 700000000 Half Duplex 525000000 HTTP Overhead 350000000 Speed limited by response request time 175000000 0 1000 10000 100000Tuesday, 5 February 13
  • 29. Tuesday, 5 February 13
  • 30. “Reducing kilobytes of data to 2 bytes ... and reducing latency form 150ms to 50ms is far more than marginal. In fact these two factors alone are enough to make Web socket seriously interesting to Google” -Ian Hickson (Google, HTML5 spec lead)Tuesday, 5 February 13
  • 31. Web socket and Security Two Schemes WS WSS (Websocket + TLS) Cross Origin Resourse Sharing (CORS) Work with exsisting Authentication SystemtsTuesday, 5 February 13
  • 32. CodeTuesday, 5 February 13
  • 33. var  socket  =  new  WebSocket("ws:// localhost:3000/websocket");   socket.onopen  =  function(){ alert(“socket  open”); }   socket.onmessage  =  function(msg){ alert(msg); }Tuesday, 5 February 13
  • 34. onOpen(); onClose(); onMessage(); onError();Tuesday, 5 February 13
  • 35. socket.send(data); socket.close();Tuesday, 5 February 13
  • 36. IssuesTuesday, 5 February 13
  • 37. Browser CompatibilityTuesday, 5 February 13
  • 38. Socket.io 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); }); }); Server Side Client Side <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>Tuesday, 5 February 13
  • 39. Socket.io Supports Internet Websockets Explore 5.5+ Adobe Flash Socket Safari 3+ AJAX Long Polling Google Chrome 4+ AJAX Multipart Streaming Firefox 3 + Forever iFrame Opera 10.61 + JSONP PollingTuesday, 5 February 13
  • 40. Go Read www.html5rocks.com/en/tutorials/ websockets/basics/ socket.io/ nodejs.org npmjs.orgTuesday, 5 February 13

×