Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Hacking websockets

3,659 views

Published on

Presentation I gave in office about Web Sockets (compared to other techniques), followed with demo of Swift client and Node.js server.

Published in: Software
  • Sex in your area is here: ♥♥♥ http://bit.ly/39pMlLF ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating direct: ❶❶❶ http://bit.ly/39pMlLF ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Hacking websockets

  1. 1. HACKING WEBSOCKETS FOR FUN AND PROFIT @tomekcejner 2015 / SmartRecruiters Inc.
  2. 2. WHAT REALTIME COMMUNICATION FULL DUPLEX LOW LATENCY
  3. 3. FOR SOCIAL FEEDS CHAT COMMON EDITING MONITORING
  4. 4. COMPARING TECHNIQUES BROWSER SERVER
  5. 5. POLLING BROWSER SERVER EVENT EVENT
  6. 6. POLLING SIMPLE ALWAYS WORKS HIGH TRAFFIC HIGH LATENCY
  7. 7. LONG POLLING BROWSER SERVER EVENT
  8. 8. LONG POLLING NEAR-REALTIME VERY LONG REQUESTS WILL BLOCK THREADS NEEDS STICKY LOADBALANCING
  9. 9. STREAMING BROWSER SERVER EVENT EVENT RESPONSE PART RESPONSE PART
  10. 10. STREAMING NEAR REALTIME BUFFERING PROXY WILL RUIN
  11. 11. WEBSOCKETS BROWSER SERVER EVENT EVENT UPGRADE
  12. 12. WEBSOCKETS REALTIME DUPLEX SUPPORTED BY LATEST BROWSERS MAY BE BROKEN BY SOME PROXIES
  13. 13. CAN I USE? http://caniuse.com/#feat=websockets
  14. 14. WEBSOCKETS API
  15. 15. var socket = new WebSocket('ws://game.example.com:12010/updates'); socket.onopen = function () { setInterval(function() { if (socket.bufferedAmount == 0) socket.send(getUpdateData()); }, 50); };
  16. 16. EMITTING MESSAGES SEND TEXT OR BINARY FRAME LOW OVERHEAD: 2 BYTES PER FRAME
  17. 17. TEXT FRAME 0x81 0x05 0x48 0x65 0x6c 0x6c 0x6f H e l l o
  18. 18. JAVASCRIPT WEBSOCKETS FRAMEWORK CLIENT WITH FALLBACKS NODE.JS SERVER
  19. 19. CODE var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); server.listen(80); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); <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> CLIENTSERVER
  20. 20. CHALLENGES STATEFULNESS SCALABILITY BROADCASTING
  21. 21. BROADCASTING io.on('connection', function(socket){ socket.join('some room'); }); io.to('some room').emit('some event'): io.on('connection', function(socket){ socket.on('say to someone', function(id, msg){ socket.broadcast.to(id).emit('my message', msg); }); });
  22. 22. MULTIPLE NODES Node A CLIENT 1 CLIENT 2 Node B CLIENT 3 CLIENT 4 ?
  23. 23. SCALING https://github.com/socketio/socket.io-redis
  24. 24. RESOURCES http://socket.io Socket.IO Swift client https://github.com/socketio/socket.io-client-swift Benefits of Web Sockets https://www.websocket.org/quantum.html Web Sockets API http://dev.w3.org/html5/websockets/ Web Sockets RFC https://tools.ietf.org/html/rfc6455 Difference between polling, long polling and web sockets explained: http://stackoverflow.com/questions/10028770/html5-websocket-vs-long-polling-vs-ajax- vs-webrtc-vs-server-sent-events http://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server- sent-events-sse-and-comet
  25. 25. THANK YOU That’s all
  26. 26. BONUS CONTENT
  27. 27. TRACKING USERS IN REDS SADD mob:online:7501234 55e83ebae4b00f589364debd SISMEMBER mob:online:7501234 55e83ebae4b00f589364debd SMEMBERS mob:online:7501234

×