WEBSOCKETS bidirectional full-duplex communication Motivation - - PowerPoint PPT Presentation

websockets
SMART_READER_LITE
LIVE PREVIEW

WEBSOCKETS bidirectional full-duplex communication Motivation - - PowerPoint PPT Presentation

CS 498RK FALL 2016 WEBSOCKETS bidirectional full-duplex communication Motivation Client Server MY BLOG HTTP POST This is my first post. ADD POST API DATABASE HTTP GET MY BLOG 02/23/15 This is my first post. NEW POST Client Server


slide-1
SLIDE 1

FALL 2016 CS 498RK

WEBSOCKETS

bidirectional full-duplex communication

slide-2
SLIDE 2

Motivation

slide-3
SLIDE 3

MY BLOG

ADD POST This is my first post.

MY BLOG

NEW POST This is my first post. 02/23/15

API DATABASE

Client Server

HTTP POST HTTP GET

slide-4
SLIDE 4

MY BLOG

ADD POST This is my first post.

MY BLOG

NEW POST This is my first post. 02/23/15

API DATABASE

Client Server

HTTP POST HTTP GET

AJAX allows server requests to be made in the background

slide-5
SLIDE 5

…but if clients don’t request, servers can’t respond!

slide-6
SLIDE 6

What types of applications need servers to push data?

slide-7
SLIDE 7

LOW-LATENCY, REAL-TIME

Multiplayer online games Chat applications Realtime updating social streams

slide-8
SLIDE 8

How can servers push data?

slide-9
SLIDE 9

POLLING

keep making requests to the server to see if there’s any new information performance problems: server has to process HUGE number of connections a second

aka faking it

slide-10
SLIDE 10

COMET

a set of more advanced techniques hidden iframes (i.e., forever frames) AJAX with long polling unstandardized hacks, performance issues

not an acronym!

en.wikipedia.org/wiki/Comet_%28programming%29

slide-11
SLIDE 11

BROWSER PLUGINS

Adobe Flash, Java raw push real-time data to clients through raw TCP socket connections with servers plug-ins not guaranteed to be installed, firewall issues

slide-12
SLIDE 12

WebSockets

slide-13
SLIDE 13

WEBSOCKETS

WebSocket API introduced in HTML5 (2009) persistent connection between the client and server send data back and forth without HTTP overhead

en.wikipedia.org/wiki/WebSocket

bidirectional full-duplex communication

slide-14
SLIDE 14

GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 Origin: http://example.com

en.wikipedia.org/wiki/WebSocket

Client Request

slide-15
SLIDE 15

HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: chat

en.wikipedia.org/wiki/WebSocket

Server Response

slide-16
SLIDE 16

var connection = new WebSocket(‘ws:// html5rocks.websocket.org/echo'); Open a WebSocket Connection

www.html5rocks.com/en/tutorials/websockets/basics/

slide-17
SLIDE 17

// When the connection is open, send some data to the server connection.onopen = function () { connection.send(‘Lorenzo Llamas'); // Send the message ‘Lorenzo Llamas' to the server }; // Log messages from the server connection.onmessage = function (e) { console.log('Server: ' + e.data); }; // Log errors connection.onerror = function (error) {/*…*/}; // Close connection connection.onclose = function(){ /*…*/ }

www.html5rocks.com/en/tutorials/websockets/basics/

Attach Event Handlers

slide-18
SLIDE 18

CROSS ORIGIN COMMUNICATION

supports communication between clients and servers on any domain server decides which domains to allow connections from

www.html5rocks.com/en/tutorials/websockets/basics/

slide-19
SLIDE 19

PROBLEMS

Immediate security concerns (Opera 11, Safari 5) Protocol was revamped and now supported by all modern browsers Incompatibility with HTTP upgrade system and some proxy servers

even if client supports it, can’t establish a connection!

slide-20
SLIDE 20

WEBSOCKETS TODAY

use libraries that use earlier fallbacks whenever WebSocket is not available socket.io

slide-21
SLIDE 21

Socket.IO

slide-22
SLIDE 22

If at first you don’t succeed…

WebSockets Adobe Flash Socket Ajax long polling Ajax multipart streaming Forever iFrame JSONP Polling

slide-23
SLIDE 23

“Socket.IO enables real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed.”

socket.io

slide-24
SLIDE 24

Demo

slide-25
SLIDE 25

<script> var socket = io(); $('form').submit(function(){ socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); }); </script>

socket.io

Client Code

slide-26
SLIDE 26

app.get('/', function(req, res){ res.sendfile('index.html'); }); io.on('connection', function(socket){ socket.on('chat message', function(msg){ io.emit('chat message', msg); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); });

socket.io

Server Code

slide-27
SLIDE 27

NEXT CLASS: USERS

courses.engr.illinois.edu/cs498rk1/