24
WebSockets The Real-Time Web, Delivered July 23, 2011 Brian McKelvey Co-Founder - Worlize Inc. twitter - @theturtle32 www.worlize.com Monday, July 25, 11

WebSockets: The Real-Time Web, Delivered

Embed Size (px)

DESCRIPTION

A technical overview of the WebSockets protocol draft-09 and background on how we got to where we are today.

Citation preview

Page 1: WebSockets: The Real-Time Web, Delivered

WebSocketsThe Real-Time Web, Delivered

July 23, 2011

Brian McKelveyCo-Founder - Worlize Inc.

twitter - @theturtle32www.worlize.com

Monday, July 25, 11

Page 2: WebSockets: The Real-Time Web, Delivered

Whiteboard Demo

• Get FireFox 7 Alpha

• http://www.mozilla.com/firefox/channel/

Monday, July 25, 11

Page 3: WebSockets: The Real-Time Web, Delivered

The Problem

•No officially supported way to do real-time communications in the browser.

Monday, July 25, 11

Page 4: WebSockets: The Real-Time Web, Delivered

Hacks, Hacks, Hacks• Polling• Long-polling• IFrame Streaming• HTMLFile (MSIE)• XHR-Multipart (Firefox)

Monday, July 25, 11

Page 7: WebSockets: The Real-Time Web, Delivered

The Solution: WebSockets

•Maintains a persistent bi-directional communications channel

• Eliminates the need for hacks

Monday, July 25, 11

Page 8: WebSockets: The Real-Time Web, Delivered

Why not TCP Sockets?

• API Simplicity• TCP is a stream-oriented protocol•We want a message-oriented protocol• Security• Scan a Local Network•DDOS Attack

Monday, July 25, 11

Page 9: WebSockets: The Real-Time Web, Delivered

WebSocket Drafts

• Started at WhatWG•WhatWG Draft-75 (old browsers)•WhatWG Draft-76 (Chrome, Safari)

• Moved to IETF•HyBi - HyperText Bi-directional Working Group•Draft-00 (Same as WhatWG Draft-76)•Draft-07 (FireFox 6 beta)•Draft-09 (FireFox 7 alpha)

Monday, July 25, 11

Page 10: WebSockets: The Real-Time Web, Delivered

The Details: Handshake

• Built on HTTP• Traverse existing proxies/intermediaries• Co-exist on same port with HTTP/HTTPS server

Monday, July 25, 11

Page 11: WebSockets: The Real-Time Web, Delivered

The Details: Handshake

GET /chat HTTP/1.1Connection: UpgradeUpgrade: websocketSec-WebSocket-Version: 8Sec-WebSocket-Key: reaiKXo+d4Hkqt45PNTlNg==Sec-WebSocket-Origin: http://piglet.worlize.com:8080Sec-WebSocket-Protocol: superchat, boringchatSec-WebSocket-Extensions: deflate-stream

Client Request

Monday, July 25, 11

Page 12: WebSockets: The Real-Time Web, Delivered

The Details: Handshake

HTTP/1.1 101 Switching ProtocolsConnection: UpgradeUpgrade: websocketSec-WebSocket-Accept: Iy0M3juCftp2uqk8/9fVWt4VfoI=Sec-WebSocket-Protocol: superchatSec-WebSocket-Extensions: deflate-stream

Server Response

Monday, July 25, 11

Page 13: WebSockets: The Real-Time Web, Delivered

The Details: Handshake• Security• Cross-Origin handling via Sec-WebSocket-Origin• Sec-WebSocket-Key Validation• Prevent cross-protocol attacks• Fail early if a dumb intermediary provides a cached

response.• Sec- headers prevent XHR from being used to contact a

WebSocket server

Monday, July 25, 11

Page 14: WebSockets: The Real-Time Web, Delivered

The Details: Masking•Most contentious issue in the HyBi Working Group.•All client-to-server messages are masked.•Why is this necessary?• Prevent cache-poisoning attacks• Prevent talking to overly permissive servers• Concern over this issue caused some browser vendors to

pull support for WebSockets draft-76.

Monday, July 25, 11

Page 15: WebSockets: The Real-Time Web, Delivered

The Details: Masking• How does it work?• Randomly generated four-byte mask key per frame• Applied using XOR to frame payload

• Reduces compression efficiency for client-to-server messages

Monday, July 25, 11

Page 16: WebSockets: The Real-Time Web, Delivered

The Details: Framing

• Simple framing protocol facilitates message oriented API• 2 to 10 bytes for frame header• Contains flags, opcode, and payload length• Payload length is a variable length field.

Monday, July 25, 11

Page 17: WebSockets: The Real-Time Web, Delivered

The Details: Framing

• Data frame types:• Binary Message• UTF-8 Text Message• Continuation

(fragmentation)

• Control frame types:• Ping• Pong• Close

Monday, July 25, 11

Page 18: WebSockets: The Real-Time Web, Delivered

The Details: Framing

• Fragmentation

• Allows control frames in the middle of a long message

• In coordination with a future extension can facilitate multiplexing.

Monday, July 25, 11

Page 19: WebSockets: The Real-Time Web, Delivered

Basic Frame Header

FIN

RSV1

RSV2

RSV3

OPCODEMSK

7-BIT LENGTH

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Byte 1 Byte 2

Frames less than 126 bytes

Monday, July 25, 11

Page 20: WebSockets: The Real-Time Web, Delivered

Medium Frame Header

FIN

RSV1

RSV2

RSV3

OPCODEMSK

Static Value "126" Indicating 16-bit length

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Byte 1 Byte 2

16-BIT LENGTH (BIG-ENDIAN UNSIGNED SHORT)

Byte 3 Byte 4

>= 126 bytes && < 65535 bytes

Monday, July 25, 11

Page 21: WebSockets: The Real-Time Web, Delivered

Long Frame Header

FIN

RSV1

RSV2

RSV3

OPCODEMSK

Static Value "127" Indicating 63-bit length

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Byte 1 Byte 2

63-BIT LENGTH (BIG-ENDIAN UNSIGNED)

> 65535 bytes

0

63-BIT LENGTH (Continued)

63-BIT LENGTH (Continued)

63-BIT LENGTH (Continued)

Monday, July 25, 11

Page 22: WebSockets: The Real-Time Web, Delivered

Server Implementations•Node.JS: WebSocket-Node• https://github.com/Worlize/WebSocket-Node

• ANSI C: libwebsockets• http://git.warmcat.com/cgi-bin/cgit/libwebsockets/

• Java: Jetty• http://webtide.intalio.com/2011/04/getting-started-with-websockets/

Monday, July 25, 11

Page 23: WebSockets: The Real-Time Web, Delivered

Client Implementations•Node.JS: WebSocket-Node• https://github.com/Worlize/WebSocket-Node

• ActionScript 3: AS3WebSocket• https://github.com/Worlize/AS3WebSocket

• ANSI C: libwebsockets• http://git.warmcat.com/cgi-bin/cgit/libwebsockets/

Monday, July 25, 11

Page 24: WebSockets: The Real-Time Web, Delivered

DemoShared Whiteboard Example

• Available in the ‘examples’ folder of:https://github.com/Worlize/WebSocket-Node

Requires FireFox 7 Alpha!

Monday, July 25, 11