websockets bring light at the end of the tunnel

31
www.spiria.com Websockets Bring Light at the End of the Tunnel Presented By JOEL LORD MidwestJs August 11 th , 2016

Upload: joel-lord

Post on 16-Feb-2017

91 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: Websockets Bring Light At The End Of The Tunnel

www.spiria.com

Websockets Bring Light at the End of the Tunnel

Presented ByJOEL LORD

MidwestJsAugust 11th, 2016

Page 2: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

JOEL LORDAbout me, eh?

• Javascript Junkie• Tinkerer• Technology enthusiast

Page 3: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Page 4: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

JOEL LORDAbout me, eh?

• Javascript Junkie• Tinkerer• Technology enthusiast

Page 5: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Agenda

• What exists and what they provide

• Performance• Pros and Cons• Real world cases• The API• Intro to socket.io• Live demos !

Page 6: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Agenda

• What exists and what they provide

• Performance• Pros and Cons• Real world cases• The API• Intro to socket.io• Live demos !

Page 7: Websockets Bring Light At The End Of The Tunnel

What Are They?WEBSOCKETS

Page 8: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

WebSocket is a protocol providing full-duplex communications channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocketAPI in Web IDL is being standardized by the W3C.

Page 9: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Typical real-timeNORMAL HTTP REQUESTS

Page 10: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Typical real-timePOLLING

Page 11: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Typical real-timeLONG-POLLING

Page 12: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Typical real-timeWEBSOCKETS

Page 13: Websockets Bring Light At The End Of The Tunnel

Typical real-timeWHAT ABOUT SERVER-SIDE EVENTS?

Page 14: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

PerformanceGIMME BENCHMARKS !

Time (in milliseconds) taken to process N messages for a constant payload

Page 15: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

PerformanceGIMME BENCHMARKS !

Time to process a fixed number of messages with varying payload size.

Page 16: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

PerformanceGIMME BENCHMARKS !

Run your own tests

Thor: https://github.com/observing/thorWebsocket-bench: https://github.com/M6Web/websocket-bench

Page 17: Websockets Bring Light At The End Of The Tunnel

Pros and Cons

Page 18: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

PROS Bi-directional data transfersFast Low bandwith Detects connection and disconnection

Page 19: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

CONS …

Page 20: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

CONS Lose the caching capabilities built in XHR and HTTPArchitectural changes in the application

Page 21: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

WebSockets in the WildREAL WORLD CASES

• Real time synchronization of data amongst a group of users (trello.com)• Live feeds (Twitter stream)• Long server processes and providing ETAs to users• Multiplayer HTML5 games• Chat clients (Slack)

Page 22: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Using WebSockets with HTML5ESTABLISHING A CONNECTION

var connection = new WebSocket('ws://somedomain/path/')

Page 23: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Using WebSockets with HTML5EVENTS

connection.onopen = function(e) { console.log("Connected");};

connection.onmessage = function(e) { console.log( "Received: " + e.data);};

connection.onclose = function(e) { console.log("Connection closed");};

Page 24: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Using WebSockets with HTML5SENDING DATA TO THE SERVER

//Stringsconnection.send('this is a string');

//Array Buffersvar image = aCanvas.getImageData(0, 0, 640, 480);var binary = new Uint8Array(image.data.length);for (var i = 0; i < image.data.length; i++) { binary[i] = image.data[i];}connection.send(binary.buffer);

//Blobsvar myFile = document.querySelector('input[type="file"]').files[0];connection.send(myFile);

//JSON objects ?var jsonObject = JSON.stringify({"data": "value"});connection.sent(jsonObject);

Page 25: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Getting StartedVARIOUS IMPLEMENTATIONS

• PubNub or Pusher (cloud)• Ratchet (PHP)• Jetty (Java)• socket.io (node.js)

Page 26: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Here comes Socket.io !WHAT’S SO COOL ABOUT IT?

• Server and client-side implementation• Falls back to long polling when necessary (IE 9 😱)• Adds features like heartbeats, timeouts, and disconnection

support not provided in WebSocket API• Easy stuff !

Page 27: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Here comes Socket.io !CLIENT SIDE

<body>

<input type="text" id="textField" /><button type="button" id="sendMsg">Send</button><textarea id="msgBox"></textarea>

</body><script src="/socket.io/socket.io.js"></script><script type="text/javascript"> var socket = io();

document.getElementById("sendMsg").onclick = function() { socket.emit("messageFromClient", { text: document.getElementById("textField").value }); };

socket.on("messageFromServer", function(data) { document.getElementById("msgBox").value += data.text; });

</script>

Page 28: Websockets Bring Light At The End Of The Tunnel

@joel__lord#midwestjs

Here comes Socket.io !SERVER SIDE

//Express server setupvar express = require("express");var app = express();var server = require("http").createServer(app);var port = 8888;

server.listen(port, function () { console.log("Server started on port " + port);});

app.use(express.static(__dirname + "/../"));

//Socket setup

var io = require("socket.io")(server);io.on("connection", function (socket) { socket.on("messageFromClient", function (data) { socket.broadcast("messageFromServer", data); });});

Page 29: Websockets Bring Light At The End Of The Tunnel

IT’S CODING TIME!LET’S GET SERIOUS

Page 30: Websockets Bring Light At The End Of The Tunnel

QUESTIONS?

Page 31: Websockets Bring Light At The End Of The Tunnel

DOCUMENT CONFIDENTIEL, TOUT DROIT RÉSERVÉ

PRESENTED BY

That’s all folks !

JOEL LORDAugust 11th, 2016

TWITTER: @JOEL__LORDGITHUB: HTTP://GITHUB.COM/JOELLORD