Upgrade to Pro — share decks privately, control downloads, hide ads and more …

High Performance WebSocket

High Performance WebSocket

Fluent 2015

Wesley Hales

April 23, 2015
Tweet

More Decks by Wesley Hales

Other Decks in Technology

Transcript

  1. @wesleyhales 1999 2015 2008 HTTP/1.1 WebSocket SSE HTTP/2 2006 2009

    SPDY 2005 AJAX 2000 REST Real Time Enablers
  2. @wesleyhales AJAX The underlying complexity is abstracted away. When you

    make an AJAX request from the browser you don’t have to think about TCP.
  3. @wesleyhales WebSocket Subprotocols, Fallbacks, Network When you send a message

    over a WebSocket connection, you have to think about everything.
  4. @wesleyhales Real Time? • Rapid client <-> server communication •

    Real time execution of events in browser • Binary data delivery
  5. @wesleyhales High Performance Hurdles • Server Side Connections • Networks

    and Proxies • Subprotocols • Time • Services vs. Frameworks
  6. @wesleyhales Subprotocols var ws = new WebSocket('wss://example.com/socket', ['appProtocol', ‘appProtocol-v2']); ws.onopen

    = function () { if (ws.protocol == 'appProtocol-v2') { ... } else { ... } } Meta data for the client <-> server transmission *No effect on the core WebSocket API
  7. @wesleyhales Time Get the data from an AJAX request in

    4 easy steps var serverTimeMillisUTC; var oReq = new XMLHttpRequest(); oReq.onload = function(){ var dateStr = oReq.getResponseHeader('Date'); serverTimeMillisUTC = new Date(Date.parse(dateStr)).getTime(); }; oReq.open("HEAD", "/?foo123", false); oReq.send(); function getServerTime() { for(tvalue in window.performance.getEntriesByType("resource")){ tresource = window.performance.getEntriesByType("resource")[tvalue]; if(tresource.name.indexOf('foo123') > 0){ return serverTimeMillisUTC + (tresource.responseEnd - tresource.responseStart); } } }
  8. @wesleyhales Time Why would I do this? Client Server Ajax

    Date Header Call Ajax Date Header Response 12:30:11 12:15:22 What is response time?
  9. @wesleyhales Time 1. Get the date header from the server

    var oReq = new XMLHttpRequest(); oReq.onload = function(){ var dateStr = oReq.getResponseHeader('Date'); serverTimeMillisUTC = new Date(Date.parse(dateStr)).getTime(); }; oReq.open("HEAD", "/?foo123", false); oReq.send(); 2. User random query param
  10. @wesleyhales Time 3. Filter on random query param if(tresource.name.indexOf('foo123') >

    0){ return serverTimeMillisUTC +
 (tresource.responseEnd - tresource.responseStart); } 4. Calculate Server Time
  11. @wesleyhales Head of Line Blocking Large messages block frame delivery

    Latency Sensitive Data? Split up the large messages Screenshots are sent as binary data over wss:// in onslyde’s dashboard client side code on github
  12. @wesleyhales Compression Extensions are managed by the browser 
 (spec

    says it’s a “framework”) wss:// connection headers 1) Chrome has implicit support (no config on client) 2) Compresses any message over 10 bytes
  13. @wesleyhales Make Sure You Need It Do you need WebSocket?

    
 HTTP2, SPDY, SSE, and WebSocket are all different. They could be combined. Are you willing to polyfill your WebSocket Implementation?
  14. @wesleyhales Browser Event Firing All Events Fire onmessage window.addEventListener('remoteMarkup', function(e)

    { var markup = JSON.parse(e.markup); document.getElementById('from-slide').innerHTML =
 decodeURIComponent(markup.remoteMarkup); }, false); onmessage: function (m) { var event = (m.data); event = (new Function("return " + event))(); event.onslydeEvent.fire(); } Remote Control listens