WebSocket
WebSocket is a technology providing for bi-directional, full-duplex communications channels, over a single Transmission Control Protocol (TCP) socket. It is designed to be implemented in web browsers and web servers, but it can be used by any client or server application. The WebSocket API is being standardized by the W3C, and the WebSocket protocol is being standardized by the IETF. Because ordinary TCP connections to ports other than 80 are frequently blocked by administrators outside of home environments, it can be used as a way to overcome these restrictions and provide similar functionality with some additional protocol overhead while multiplexing several WebSocket services over a single TCP port.
For the client side, WebSocket was implemented in Firefox 4, Google Chrome 4, Opera 11, and Safari 5, as well as the mobile version of Safari in iOS 4.2.[1] Also, the BlackBerry Browser in OS7 supports WebSockets.[2] However, although present, support was disabled by default in Firefox 4 and 5 and Opera 11 because of concerns over security vulnerabilities.[3][4] The new -07 version of the WebSocket protocol, which fixes the protocol bug, is implemented and enabled by default in Firefox 6 [5] and the upcoming release of Chromium 14.[6]
WebSocket Protocol Handshake
To establish a WebSocket connection, the client sends a WebSocket handshake request, and the server sends a WebSocket handshake response, as shown in the following example:
draft-ietf-hybi-thewebsocketprotocol-00
This is the older handshake mechanism; see below for newer versions.
Browser request to the server:
GET /demo HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: example.com
Origin: http://example.com
Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
^n:ds[4U
Server response:
HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: http://example.com
Sec-WebSocket-Location: ws://example.com/demo
Sec-WebSocket-Protocol: sample
8jKS'y:G*Co,Wxa-
The Sec-WebSocket-Key1 and Sec-WebSocket-Key2 fields and the 8 bytes after the fields are random tokens which the server uses to construct a 16-byte token at the end of its handshake to prove that it has read the client's handshake.
The handshake is constructed by concatenating the numbers from the first key, and dividing by the number of spaces. This is then repeated for the second key. The two resulting numbers are concatenated with each other, and with the last 8 bytes after the fields. The final result is an MD5 sum of the concatenated string.[7]
The handshake looks like HTTP but actually isn't. It allows the server to interpret part of the handshake request as HTTP and then switch to WebSocket.
Once established, WebSocket data frames can be sent back and forth between the client and the server in full-duplex mode. Text frames can be sent full-duplex, in either direction at the same time. The data is minimally framed with just two bytes. Each frame starts with a 0x00 byte, ends with a 0xFF byte, and contains UTF-8 data in between. Binary frames are not supported yet in the API. WebSocket text frames use a terminator, while binary frames use a length prefix.
draft-ietf-hybi-thewebsocketprotocol-06
To establish a WebSocket connection, the client sends a WebSocket handshake request, and the server sends a WebSocket handshake response, as shown in the following example:
GET /ws HTTP/1.1
Host: pmx
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Version: 6
Sec-WebSocket-Origin: http://pmx
Sec-WebSocket-Extensions: deflate-stream
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Server response:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
The client sends a Sec-WebSocket-Key which is base64 encoded. To this key the magic string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" is appended, hashed with SHA1 and then base64 encoded. Notice that the Sec-WebSocket-Key is base64 encoded but is not decoded by the server. The result is then replied in the header "Sec-WebSocket-Accept".
Sec-WebSocket-Key to Sec-WebSocket-Accept example :
- "x3JJHMbDL1EzLkh9GBhXDw==258EAFA5-E914-47DA-95CA-C5AB0DC85B11" string hashed by SHA1 gives "1d29ab734b0c9585240069a6e4e3e91b61da1969" hexadecimal value.
- Unix command `printf "\x1d\x29\xab\x73\x4b\x0c\x95\x85\x24\x00\x69\xa6\xe4\xe3\xe9\x1b\x61\xda\x19\x69" | base64` prints "HSmrc0sMlYUkAGmm5OPpG2HaGWk="
Proxy traversal
WebSocket protocol client implementations try to detect if the user agent is configured to use a proxy when connecting to destination host and port and, if it is, uses HTTP CONNECT method to set up a persistent tunnel.
While the WebSocket protocol itself is unaware of proxy servers and firewalls, it features an HTTP-compatible handshake so that HTTP servers can share their default HTTP and HTTPS ports (80 and 443) with a WebSocket gateway or server. The WebSocket protocol defines a ws:// and wss:// prefix to indicate a WebSocket and a WebSocket Secure connection, respectively. Both schemes use an HTTP upgrade mechanism to upgrade to the WebSocket protocol. Some proxy servers are harmless and work fine with WebSocket; others will prevent WebSocket from working correctly, causing the connection to fail. In some cases additional proxy server configuration may be required, and certain proxy servers may need to be upgraded to support WebSocket.
If unencrypted WebSocket traffic flows through an explicit or a transparent proxy server on its way to the WebSocket server, then, whether or not the proxy server behaves as it should, the connection is almost certainly bound to fail today (as WebSocket become more mainstream, proxy servers may become WebSocket aware). Therefore, unencrypted WebSocket connections should be used only in the simplest topologies.[8]
If an encrypted WebSocket connection is used, then the use of Transport Layer Security (TLS) in the WebSocket Secure connection ensures that an HTTP CONNECT command is issued when the browser is configured to use an explicit proxy server. This sets up a tunnel, which provides low-level end-to-end TCP communication through the HTTP proxy, between the WebSocket Secure client and the WebSocket server. In the case of transparent proxy servers, the browser is unaware of the proxy server, so no HTTP CONNECT is sent. However, since the wire traffic is encrypted, intermediate transparent proxy servers may simply allow the encrypted traffic through, so there is a much better chance that the WebSocket connection will succeed if WebSocket Secure is used. Using encryption is not free of resource cost, but often provides the highest success rate.
A mid-2010 draft (version hixie-76) broke compatibility with reverse-proxies and gateways by including 8 bytes of key data after the headers, but not advertising that data in a Content-Length: 8 header.[9] This data was not forwarded by all intermediates, which could lead to protocol failure. More recent drafts (e.g., hybi-09[10]) put the key data in a Sec-WebSocket-Key header, solving this problem.
URL scheme
The WebSocket protocol specification defines two new URI schemes, ws: and wss:,[11] for unencrypted and encrypted connections respectively. Apart from the scheme name, the rest of the URI components are defined to use URI generic syntax.[12]
Browser support
Firefox 7/8 and Chrome 14 are currently the only browsers supporting the latest draft specification of the WebSockets protocol. A detailed protocol test suite report [13] lists the conformance of those browsers to specific protocol aspects.
The following browsers originally supported WebSocket, but have since disabled the protocol by default. Chrome also plans to disable the WebSocket if actual exploit code appears before the protocol is revised.[14]
Currently, two browsers still support the outdated draft-ietf-hybi-thewebsocketprotocol-00. It has been disabled in Firefox and Opera due to security issues.
Microsoft supports the draft-ietf-hybi-thewebsocketprotocol-04 in Internet Explorer through a prototype HTML5 Labs.[17]
Protocol | Internet Explorer | Mozilla Firefox | Google Chrome | Safari | Opera | NetFront |
---|---|---|---|---|---|---|
hixie-75 | 4 | 5.0.0 | ||||
hixie-76 hybi-00 |
4.0 (DISABLED) | 6 | 5.0.1 | 11.00 (DISABLED) | ||
hybi-06 | HTML5 Labs[18] | dev[19] | ||||
hybi-07 | 6.0[20] | |||||
hybi-09 | HTML5 Labs[21] | |||||
hybi-10 | 7[22] | 14[23] |
Libraries
- Autobahn Python/Twisted-based WebSockets (Clients & Servers), Java/Android (under development)
- CoreWebSocket Core Foundation based WebSocket library for iOS and Mac OSX by Mirek Rusin
- UnittWebSocketClient Objective-C WebSocket library for iOS (supports rev00, rev07, rev08, rev10, binary & text messages, message fragments, etc)
- weberknecht Java WebSocket library for Android
- libwebsockets C WebSocket Server Library
- WebSocket-Node - a pure JavaScript client & server implementation for Node.JS
- [1] - Ruby/EventMachine WebSocket server
- Cramp - Ruby/EventMachine asynchronous web framework which implements WebSocket and Server-Sent Events
- JBoss Netty – asynchronous event-driven Java New I/O (NIO) network application framework supporting WebSockets
- Union Platform – JavaScript-extensible Java server and JavaScript WebSocket client library for creating connected applications
- Alchemy Websockets – Lightweight microthreaded C# WebSocket server and client library
- Jetty - Java HTTP Server and Client with support for WebSockets client and server for J2SE and Android
See also
- Comparison of WebSocket implementations
- XMLHttpRequest
- Push technology
- Comet
- Server-sent events
- BOSH
- Ian Hickson
References
![]() |
- ^ Katie Marsal (November 23, 2010). "Apple adds accelerometer, WebSockets support to Safari in iOS 4.2". AppleInsider.com. Retrieved 2011-05-09.
- ^ "Web Sockets API". RIM. Retrieved 8 July 2011.
- ^ Chris Heilmann (December 8, 2010). "WebSocket disabled in Firefox 4". Hacks.Mozilla.org. Retrieved 2011-05-09.
- ^ Aleksander Aas (December 10, 2010). "Regarding WebSocket". My Opera Blog. Retrieved 2011-05-09.
- ^ Dirkjan Ochtman (May 27, 2011). "WebSocket enabled in Firefox 6". Mozilla.org. Retrieved 2011-06-30.
- ^ "Chromium Web Platform Status". Retrieved 2011-08-03.
- ^ "The WebSocket protocol". August 16, 2010. Retrieved 2011-05-09.
- ^ How Web Sockets Interact With Proxy Servers
- ^ WebSocket -76 is incompatible with HTTP reverse proxies
- ^ "The WebSocket protocol, draft hybi-09". June 13, 2011. Retrieved June 15, 2011.
- ^ IANA Uniform Resource Identifer (URI) Schemes
- ^ http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol
- ^ WebSockets Protocol Test Report
- ^ Issue 5643005: Disable WebSocket by default
- ^ How to enable WebSockets in Firefox
- ^ Opera 11 release notes, with procedure to re-enable Web Sockets
- ^ Microsoft's Interoperability Labs releases WebSockets-04
- ^ Latest WebSockets Release Interoperates with Firefox, Eclipse's Jetty - Interoperability @ Microsoft - Site Home - MSDN Blogs
- ^ hybi Firefox with WebSockets -06 demo build
- ^ Bug 640003 - WebSockets - upgrade to ietf-06+
- ^ The WebSockets Prototype Gets Another Update
- ^ WebSockets - upgrade to ietf-07>
- ^ Chromium bug 64470
External links
- IETF Hypertext-Bidirectional (HyBi) working group
- The WebSocket protocol - Internet-Draft at IETF
- The WebSocket protocol (Network Working Group) - Old protocol
- The WebSocket API - W3C draft specification of the API
- WebSockets.org - a web-site dedicated to WebSockets
- WebSocketsTest.com - a web-site to test WebSockets support and operation in your browser.
- HTML5 Server-Push Technologies, Part 2 - explains WebSockets
- Websockets in HTML5 - Easy to understand read on WebSockets
- HTML5 WebSockets: An example with jetty and chrome - A complete example on WebSockets with jetty.
- Web Socket API in BlackBerry Browser with OS7