the realtime web http 1 1 to websocket spdy and beyond
play

The realtime web: HTTP/1.1 to WebSocket, SPDY and beyond Guillermo - PowerPoint PPT Presentation

The realtime web: HTTP/1.1 to WebSocket, SPDY and beyond Guillermo Rauch @ QCon . November 2012. Guillermo . CTO and co-founder at LearnBoost . Creator of socket.io and engine.io . @ rauchg on twitter http:// devthought.com Author of Wileys


  1. The realtime web: HTTP/1.1 to WebSocket, SPDY and beyond Guillermo Rauch @ QCon . November 2012.

  2. Guillermo .

  3. CTO and co-founder at LearnBoost .

  4. Creator of socket.io and engine.io .

  5. @ rauchg on twitter

  6. http:// devthought.com

  7. Author of Wiley’s Smashing Node http://smashingnode.com

  8. The realtime web

  9. HTTP over TCP/IP

  10. No specific techniques or protocols

  11. Realtime = fast data exchange .

  12. Let's analyze the timeline of a typical web request.

  13. https://mywebsite.com

  14. 1. We first need to look up the DNS record for mywebsite.com

  15. DNS. Low bandwidth . Potentially high latency .

  16. Browsers have different approaches and optimizations.

  17. Our best shot at optimizing this is <link rel="dns-prefetch"> .

  18. 2. http vs https .

  19. “In January this year (2010), Gmail switched to using HTTPS for everything by default […] In our production frontend machines, SSL/TLS accounts for less than 1% of the CPU load, less than 10KB of memory per connection and less than 2% of network overhead .” http://www.imperialviolet.org/2010/06/25/overclocking-ssl.html (emphasis mine)

  20. SSL allows us to upgrade the protocol without breaking outdated proxies (eg: WebSocket).

  21. 3 . Request headers 1. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 2. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 3. Accept-Encoding: gzip,deflate,sdch 4. Accept-Language: en-US,en;q=0.8 5. Cache-Control: max-age=0 6. Connection: keep-alive 7. Host: www.imperialviolet.org 8. If-Modified-Since: Tue, 06 Nov 2012 21:22:53 GMT 9. User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.5 Safari/537.17

  22. Consider a character-by-character realtime editor (GDocs).

  23. In the following example we send the character “ a ”.

  24. Headers 1. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 2. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 3. Accept-Encoding: gzip,deflate,sdch 4. Accept-Language: en-US,en;q=0.8 5. Cache-Control: max-age=0 6. Connection: keep-alive 7. Host: www.myhost.com 8. User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.5 Safari/537.17 Body (JSON obj) 1. { “c”: “a” }

  25. HTTP/1.1 overhead: 370~ bytes per character. Boo .

  26. This only considers sending data.

  27. How ‘bout getting data?

  28. We poll the server through a GET request

  29. When the server responds, we send another one.

  30. And another one.

  31. …and another one. Boo .

  32. If the server has no data to send us, we would be generating a LOT of traffic.

  33. Instead, we make the server wait a bit if it has no data.

  34. This is what we call long-polling .

  35. Pseudo-code time.

  36. function send(data){ var xhr = new XMLHttpRequest(); xhr.open(‘POST’, ‘/’, false); xhr.send(data); } function get(fn) { var xhr = new XMLHttpRequest(); xhr.open(‘GET’, ‘/’, false); xhr.send(data); xhr.onreadystatechange = function(){ if (200 == xhr.status) { fn(); // send data to user get(); // restart poll } } }

  37. Most applications need to send data in a particular order .

  38. For our example, the order that the user types.

  39. Therefore, to replicate socket semantics we need to implement buffering . Boo .

  40. For example, the user types a then b c d every 100ms.

  41. If an existing send() is in-flight…

  42. We accumulate the other data packets in an array.

  43. Then we send that over when the server replies.

  44. But instead of just sending one request per character.

  45. We try to create a data packet with “b” “c” and “d” to minimize the request headers traffic .

  46. Headers 1. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 2. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 3. Accept-Encoding: gzip,deflate,sdch 4. Accept-Language: en-US,en;q=0.8 5. Cache-Control: max-age=0 6. Connection: keep-alive 7. Host: www.myhost.com 8. User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.5 Safari/537.17 Body (JSON Array) 1. [{ “c”: “b” }, { “c”: “c” }, { “c”: “d” }]

  47. This means, however, that we need a protocol .

  48. A protocol on top of a protocol (HTTP).

  49. Server and client need to agree that the data is not simply the request body , but within it.

  50. This is a lot of work before you can focus on your app!

  51. This has been a common trend in web development.

  52. Need rounded corners?

  53. Circa 2001 .wrap-inner { <div class=”wrap-outer”> .wrap-outer { background: …; <div class=”wrap”> background: …; padding: …; <div class=”wrap-inner”> padding: …; } <div class=”content”> } </div> .wrap { </div> background: …; </div> padding: …; </div> }

  54. Now .content { <div class=”content”> border-radius: 3px; </div> }

  55. For the realtime web…

  56. Circa 2001 - Data buffering on server within GET polls. - Data buffering on client. - JSON protocol or equivalent. - XMLHttpRequest fallbacks - Cross-domain tricks - …

  57. Now - WebSocket!

  58. Simpler client side API.

  59. var ws = new WebSocket(); ws.onopen = function(){}; ws.onclose = function(){}; ws.onmessage = function(){};

  60. Light-weight protocol .

  61. Instead of sending many HTTP requests.

  62. Browser sends one that contains a header: Upgrade: WebSocket

  63. The connection is then taken over.

  64. Like TCP but unlike HTTP , it’s bi-directional .

  65. We can send and receive over the same socket.

  66. In terms of our realtime app example…

  67. WebSocket overhead: 2-5~* bytes per character. * depending on spec

  68. But WS is buggy on mobile, unavailable in older browsers, not understood by all middleware, breaks often without SSL.

  69. Meet engine.io , the data transport engine of socket.io

  70. var ws = new eio.Socket(); ws.onopen = function(){}; ws.onclose = function(){}; ws.onmessage = function(){};

  71. Same API, long-polling first, upgrades to WS if possible.

  72. The main advantage of WS is lightweight framing!

  73. Most of the benefits that WS offers, like lower-bandwidth, would still benefit XMLHttpRequest.

  74. Meet SPDY .

  75. Headers overhead?

  76. “SPDY compresses request and response HTTP headers, resulting in fewer packets and fewer bytes transmitted.” http://www.chromium.org/spdy/spdy-whitepaper

  77. TCP overhead?

  78. “SPDY allows for unlimited concurrent streams over a single TCP connection.” http://www.chromium.org/spdy/spdy-whitepaper

  79. SPDY works transparently to your app.

  80. SPDY assumes SSL.

  81. Just upgrade your SSL terminator or load balancer and reap the benefits!

  82. In other words, if you host your app on Heroku…

  83. Keep doing what you’re doing, and wait for the upgrade.

  84. But wait, many apps still need socket-like semantics .

  85. I want to write “ socket.send(‘data’); ” instead of “ new XMLHttpRequest ”

  86. I’m not even sending XML !@!!@

  87. Solution: WebSocket layering over SPDY/3.

  88. “ With this protocol, WebSocket API get both benefits of usable bi-directional communication API, and a fast, secure, and scalable multiplexing mechanism on the top of SPDY ” https://docs.google.com/document/d/1zUEFzz7NCls3Yms8hXxY4wGXJ3EEvoZc3GihrqPQcM0/edit#

  89. Currently a draft.

  90. SPDY/3 is currently experimental and available through chrome://flags .

  91. Solution until the ideal world occurs: engine.io

  92. Moving forward… Most applications are not data-packet oriented.

  93. socket.io builds on top of engine.io to offer easy-to-use realtime events for everyone.

  94. var socket = io.connect(); socket.on(‘character’, function(){ // render! }); socket.on(‘chat message’, function(){ // render! }); socket.emit(‘ready!’);

  95. The goal is to not just focus on the speed of data transfer but also speed of development .

  96. While ensuring the reliability that the data can be exchanged in the fastest way, in every network , on every device .

  97. What’s next?

  98. - Raw TCP / UDP sockets?

  99. chrome.experimental.socket.create('tcp', '127.0.0.1', 8080, function(socketInfo) { chrome.experimental.socket.connect(socketInfo.socketId, function (result) { chrome.experimental.socket.write(socketInfo.socketId, "Hello, world!"); }); });

  100. For now, only for extensions.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend