48
Copyright © 2011 - Kaazing Corporation. All rights reserved. Building next-gen Web Apps with WebSocket

Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Building next-gen Web Apps with WebSocket

Page 2: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Who am I? •  Graham Gear •  Solution Architect,

with Kaazing, purveyors of HTML5 enabling tech

•  Based in London, but Australian

•  Been building distributed applications for 10 years

•  Thanks for listening to me in English ;)

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 3: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Agenda •  Client-Server Architecture •  Web Middleware Architecture •  WebSocket Architecture •  WebSocket in Detail

o API o Wire protocol o Proxies o  Load-balancers o Support

•  WebSocket Examples

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 4: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Client-Server Architecture

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 5: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

HTTP Is Not Full Duplex

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 6: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Half-Duplex Architecture

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 7: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Legacy Web Stack •  Designed to serve static documents

•  HTTP •  Half duplex communication

•  High latency •  Bandwidth intensive

•  HTTP header traffic approx. 800 to 2000 bytes overhead per request/response

•  Complex architecture •  Not changed since the 90’s •  Plug-ins •  Polling / long polling

•  Real-time is hard •  Expensive to “Webscale” applications

Half duplex Full duplex

Page 8: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Polling •  Polling is "nearly real-time" •  Used in Ajax applications to simulate real-time

communication •  Browser sends HTTP requests at regular

intervals and immediately receives a response

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 9: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Polling Architecture

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 10: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Long Polling •  Also known as asynchronous polling •  Browser sends a request to the server and the

server keeps the request open for a set period •  HTTP headers, present in both long-polling

and polling often account for most of the network traffic

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 11: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Long Polling Architecture

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 12: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Streaming •  More efficient, but sometimes problematic •  Possible complications:

o Proxies and firewalls o Response builds up and must be flushed

periodically o Cross-domain issues to do with browser

connection limits

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 13: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Streaming Architecture

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 14: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

GET /PollingStock//PollingStock HTTP/1.1 Host: localhost:8080 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://localhost:8080/PollingStock/ Cookie: showInheritedConstant=false; showInheritedProtectedConstant=false; showInheritedProperty=false; showInheritedProtectedProperty=false; showInheritedMethod=false; showInheritedProtectedMethod=false; showInheritedEvent=false; showInheritedStyle=false; showInheritedEffect=false;

HTTP Request Headers

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Client

Page 15: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

HTTP Response Headers

Copyright © 2011 - Kaazing Corporation. All rights reserved.

•  Total (unnecessary) HTTP request and response header information overhead: 871 bytes (example)

•  Overhead can be as much as 2000 bytes

HTTP/1.x 200 OK X-Powered-By: Servlet/2.5 Server: Sun Java System Application Server 9.1_02 Content-Type: text/html;charset=UTF-8 Content-Length: 321 Date: Sat, 07 Nov 2009 00:32:46 GMT

Server

Page 16: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

HTTP Header Traffic Analysis •  Example network throughput for HTTP

request and response headers associated with polling •  Use case A: 1,000 clients polling every second:

•  Network throughput is (871 x 1,000) = 871,000 bytes = 6,968,000 bits per second (~6.6 Mbps)

•  Use case B: 10,000 clients polling every second: •  Network throughput is (871 x 10,000) = 8,710,000 bytes =

69,680,000 bits per second (~66 Mbps)

•  Use case C: 100,000 clients polling every second: •  Network throughput is (871 x 100,000) = 87,100,000

bytes = 696,800,000 bits per second (~665 Mbps)

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 17: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Comet Problems

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 18: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Complexity does not scale

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 19: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Enter HTML5 WebSocket!

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 20: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

HTML5 WebSocket •  W3C API and IETF Protocol •  Full-duplex, single socket •  Enables web pages to communicate with a

remote host •  Traverses firewalls, proxies, and routers

seamlessly •  Leverages Cross-Origin Resource Sharing

(CORS) •  Share port with existing HTTP content

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 21: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

HTML5 WebSocket Schemes •  WebSocket

o  ws://www.websocket.org/text •  WebSocket Secure

o  wss://www.websocket.org/encrypted-text

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 22: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Desktop vs. Browser •  Desktop Networking

•  Full-duplex bidirectional TCP sockets •  Access any server on the network

•  Browser Networking •  Half-duplex HTTP request-response •  HTTP polling, long polling fraught with problems

•  Next-gen Browser Networking o Half-duplex HTTP request-response o  Full duplex, bidirectional WebSockets

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 23: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Desktop Architecture

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 24: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

WebSocket Architecture

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 25: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

The New Web Stack •  Designed for full-duplex high

performance transactional Web •  HTTP & HTML5 WebSocket •  Full duplex communication

•  Lower latency •  Reduced bandwidth •  Simplified architecture •  Massive scalability

Half duplex Full duplex

Page 26: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

HTML5 WebSocket •  Connection established by upgrading from the

HTTP protocol to the WebSocket protocol using the same TCP connection

•  Once upgraded, WebSocket data frames can be sent back and forth between the client and the server in full-duplex mode

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 27: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

WebSocket Handshake

Copyright © 2011 - Kaazing Corporation. All rights reserved.

GET /text HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: www.example.com Origin: http://example.com WebSocket-Protocol: sample …\r\n

Client

HTTP/1.1 101 WebSocket Protocol Handshake Upgrade: WebSocket Connection: Upgrade WebSocket-Origin: http://example.com WebSocket-Location: ws://example.com/demo WebSocket-Protocol: sample …\r\n

Server

Page 28: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

HTML5 WebSocket Frames •  Frames can be sent full-duplex, in either

direction at any the same time •  Each frame of data:

o Starts with a 0x00 byte and End with a 0xFF byte o Contains UTF-8 data in between

•  Example: o  \x00Hello, WebSocket\0xff

•  There is no defined maximum size o  If the user agent has content that is too large to be

handled, it must fail the WebSocket connection o  JavaScript does not allow >4GB of data, so that is

a practical maximum Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 29: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Reduce Network Traffic •  With WebSocket, each frame has only 2 bytes

of packaging (a 500:1 or even 1000:1 reduction)

•  No latency involved in establishing new TCP connections for each HTTP message

•  Dramatic reduction in unnecessary network traffic and latency

•  Remember the Polling HTTP header traffic? 665 Mbps network throughput for just headers

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 30: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

WebSocket Framing Analysis •  Example network throughput overhead

associated with WebSocket framing •  Use case A: 1,000 clients receive 1 message per

second: Network throughput is (2 x 1,000) = 2,000 bytes = 16,000 bits per second (~0.015 Mbps)

•  Use case B: 10,000 clients receive 1 message per second: Network throughput is (2 x 10,000) = 20,000 bytes = 160,000 bits per second (~0.153 Mbps)

•  Use case C: 100,000 clients receive 1 message per second: Network throughput is (2 x 100,000) = 200,000 bytes = 1,600,000 bits per second (~1.526 Mbps)

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 31: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Polling vs. WebSocket

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 32: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Latency Reduction

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 33: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Overheard… "Reducing kilobytes of data to 2 bytes…and

reducing latency from 150ms to 50ms is far more than marginal. In fact, these two factors alone are enough to make WebSocket seriously interesting to Google." —Ian Hickson (Google, HTML5 spec lead)

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 34: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Using the WebSocket API

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 35: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

//Checking for browser support if (window.WebSocket) { document.getElementById("support").innerHTML = "HTML5 WebSocket is supported"; } else { document.getElementById("support").innerHTML = "HTML5 WebSocket is not supported"; }

JavaScript

Check Browser Support

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 36: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

//Create new WebSocket var mySocket = new WebSocket("ws://www.WebSocket.org"); // Associate listeners mySocket.onopen = function(evt) {

alert("Connection open…"); }; mySocket.onmessage = function(evt) {

alert("Received message: " + evt.data); }; mySocket.onclose = function(evt) {

alert("Connection closed…"); };

JavaScript

Using the WebSocket API

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 37: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

// Sending data mySocket.send("WebSocket Rocks!"); // Close WebSocket mySocket.close();

JavaScript

Using the WebSocket API

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 38: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Extending WebSocket •  Once you have WebSocket, you can

communicate with WebSocket Servers and back-end servers and directly with message brokers

•  You can extend client-server protocols to the web: •  XMPP, Jabber •  Pub/Sub (Stomp/AMQP) •  Gaming protocols •  Any TCP-based protocol

•  Browser becomes a first-class network communication citizen Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 39: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Websocket Intermediaries •  Web Socket protocol itself is unaware of proxy

servers and firewalls •  Features an HTTP-compatible handshake so

that HTTP servers can share their default HTTP and HTTPS ports (80 and 443) with a WebSocket server

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 40: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Types of Proxy Servers

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 41: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Proxy Servers •  Problems for web applications that have a

long-lived connection •  May also buffer unencrypted HTTP

responses, thereby introducing unpredictable latency during HTTP response streaming

•  Without any intermediary servers, a WebSocket connection can be established smoothly, as long as both the server and the client understand the WebSocket protocol

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 42: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Proxy Traversal Tree

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 43: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Load Balancing Routers •  Two Types:

1.  TCP (layer-4) load-balancing routers should work well with WebSockets, because they have the same connection profile: connect once up front and stay connected, rather than the HTTP document transfer request-response profile

2.  HTTP (Layer 7) load-balancing routers expect HTTP traffic and can easily get confused by WebSocket upgrade traffic. For that reason, Layer 7 load balancing routers may need to be configured to be explicitly aware of WebSocket traffic

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 44: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Firewalls •  Since firewalls normally just enforce the rules

for inbound traffic rejection and outbound traffic routing (for example, through the proxy server), there are usually no specific WebSocket traffic-related firewall concerns

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 45: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Server Support •  Kaazing WebSocket Gateway

•  Highly optimized, transparent emulation •  Reliability through proxies and load balancers •  Layered protocol gateway and client side support •  Enterprise scalability, security and reliability

•  Apache mod_pywebsocket •  Jetty •  phpwebsockets •  web-socket-ruby •  Yaws (Erlang) ….

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 46: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Browser Support •  Chrome 4.0+ •  Safari 5.0+ •  Firefox 4.0+ (Disabled by default) •  Opera 11.0+ (Disabled by default) •  Internet Explorer 9.x/10.0 (9.0 Beta)

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 47: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Learn More… •  Apress book: Pro HTML5 Programming

(P. Lubbers, B. Albers, & F. Salim) http://www.prohtml5.com/

•  HTML5 WebSocket: o  http://www.websockets.org o  http://kaazing.me (WebSocket demo)

•  Kaazing Corporation – Training: o E-mail us: [email protected] o Web site: http://tech.kaazing.com/training/

Copyright © 2011 - Kaazing Corporation. All rights reserved.

Page 48: Building next-gen Web Apps with WebSocket · 2016. 1. 15. · The New Web Stack • Designed for full-duplex high performance transactional Web • HTTP & HTML5 WebSocket • Full

Examples and Q&A

Copyright © 2011 - Kaazing Corporation. All rights reserved.