World Library  
Flag as Inappropriate
Email this Article


Article Id: WHEBN0025553718
Reproduction Date:

Title: WebSocket  
Author: World Heritage Encyclopedia
Language: English
Subject: Web-based SSH, Java servlet, View-source URI scheme, WebGL, Hypertext Transfer Protocol
Publisher: World Heritage Encyclopedia


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 WebSocket API in Web IDL is being standardized by the W3C.

WebSocket is designed to be implemented in web browsers and web servers, but it can be used by any client or server application. The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request.[1] The WebSocket protocol makes more interaction between a browser and a web site possible, facilitating live content and the creation of real-time games. This is made possible by providing a standardized way for the server to send content to the browser without being solicited by the client, and allowing for messages to be passed back and forth while keeping the connection open. In this way a two-way (bi-directional) ongoing conversation can take place between a browser and the server. A similar effect has been achieved in non-standardized ways using stop-gap technologies such as Comet.

In addition, the communications are done over TCP port number 80, which is of benefit for those environments which block non-web Internet connections using a firewall. The WebSocket protocol is currently supported in most major browsers including Google Chrome, Internet Explorer, Firefox, Safari and Opera. WebSocket also requires web applications on the server to support it.

Technical overview

Unlike HTTP, WebSocket provides full-duplex communication. Additionally, WebSocket enables streams of messages on top of TCP. TCP alone deals with streams of bytes with no inherent concept of a message. Before WebSocket, port 80 full-duplex communication was attainable using Comet channels; however, Comet implementation is nontrivial, and due to the TCP handshake and HTTP header overhead, it is inefficient for small messages. WebSocket protocol aims to solve these problems without compromising security assumptions of the web.

Browser implementation

A secure version of the WebSocket protocol is implemented in Firefox 6,[2] Safari 6, Google Chrome 14,[3] Opera 12.10 and Internet Explorer 10.[4]

An older, less secure version of the protocol was implemented in Opera 11 and Safari 5, as well as the mobile version of Safari in iOS 4.2.[5] Also, the BlackBerry Browser in OS7 implements WebSocket.[6] Because of vulnerabilities, it was disabled in Firefox 4 and 5,[7] and Opera 11.[8]

WebSocket protocol handshake

To establish a WebSocket connection, the client sends a WebSocket handshake request, for which the server returns a WebSocket handshake response, as shown in the following example:[9]:section 1.2

Client request:

GET /chat HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13

Server response:

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

The handshake resembles HTTP so that servers can handle HTTP connections as well as WebSocket connections on the same port. However the specific fields involved, and what follows after the handshake, do not conform to the HTTP protocol.

Note that each line ends with an EOL (end of line) sequence, \r\n. There must be an extra blank line at the end.

The client sends a Sec-WebSocket-Key which is a random value that has been base64 encoded. To form a response, the GUID 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 is appended to this base64 encoded key. The base64 encoded key will not be decoded first.[10] The resulting string is then hashed with SHA-1, then base64 encoded. Finally, the resulting reply occurs in the header Sec-WebSocket-Accept.

Once the connection is established, the client and server can send WebSocket data or text frames back and forth in full-duplex mode. The data is minimally framed, with a small header followed by payload. WebSocket transmissions are described as "messages", where a single message can optionally be split across several data frames. This can allow for sending of messages where initial data is available but the complete length of the message is unknown (it sends one data frame after another until the end is reached and marked with the FIN bit). With extensions to the protocol, this can also be used for multiplexing several streams simultaneously (for instance to avoid monopolizing use of a socket for a single large payload).

It is important (from a security perspective) to validate the "Origin" header during the connection establishment process on the serverside (against the expected origins) to avoid Cross-Site WebSocket Hijacking attacks, which might be possible when the connection is authenticated with Cookies or HTTP authentication. It is better to use tokens or similar protection mechanisms to authenticate the WebSocket connection when sensitive (private) data is being transferred over the WebSocket.[11]

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 transparent 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.[12]

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 since it would be travelling through a secure tunnel.

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.[13] This data was not forwarded by all intermediates, which could lead to protocol failure. More recent drafts (e.g., hybi-09[14]) put the key data in a Sec-WebSocket-Key header, solving this problem.

URI scheme

The WebSocket protocol specification defines two new URI schemes, ws: and wss:,[15] for unencrypted and encrypted connections respectively. Apart from the scheme name, the rest of the URI components are defined to use URI generic syntax.[16]

Browser support

All the latest browsers support the latest specification (RFC 6455) of the WebSocket protocol. A detailed protocol test suite report[17] lists the conformance of those browsers to specific protocol aspects.

Implementation status
Protocol Draft date Internet Explorer Firefox[18] (PC) Firefox (Android) Chrome (PC, Mobile) Safari (Mac, iOS) Opera (PC, Mobile) Android Browser
hixie-75 February 4, 2010 4 5.0.0
May 6, 2010
May 23, 2010
4.0 (disabled) 6 5.0.1 11.00 (disabled)
7 hybi-07 April 22, 2011 6[19]1
8 hybi-10 July 11, 2011 7[20]1 7 14[21]
13 RFC 6455 December, 2011 10[22] 11 11 16[23] 6 12.10[24] 4.4

1 Gecko-based browsers versions 6–10 implement the WebSocket object as "MozWebSocket",[25] requiring extra code to integrate with existing WebSocket-enabled code.

Experimental extensions

There is also a command-line switch for Google Chrome (--enable-websocket-over-spdy) that enables an early experimental implementation of WebSocket over SPDY.[26]


Using the Google Chrome Developer Tools, developers can inspect the WebSocket handshake as well as the WebSocket frames.[27]

See also


  1. ^ RFC 6455#section-1.7
  2. ^ Dirkjan Ochtman (May 27, 2011). "WebSocket enabled in Firefox 6". Retrieved 2011-06-30. 
  3. ^ "Chromium Web Platform Status". Retrieved 2011-08-03. 
  4. ^ "WebSockets (Windows)". Microsoft. 2012-09-28. Retrieved 2012-11-07. 
  5. ^ Katie Marsal (November 23, 2010). "Apple adds accelerometer, WebSockets support to Safari in iOS 4.2". Retrieved 2011-05-09. 
  6. ^ "Web Sockets API". RIM. Retrieved 8 July 2011. 
  7. ^ Chris Heilmann (December 8, 2010). "WebSocket disabled in Firefox 4". Retrieved 2011-05-09. 
  8. ^ Aleksander Aas (December 10, 2010). "Regarding WebSocket". My Opera Blog. Archived from the original on 2010-12-15. Retrieved 2011-05-09. 
  9. ^ "RFC 6455 - The WebSocket Protocol". Retrieved 2014-03-21. 
  10. ^
  11. ^ "Article about Cross-Site WebSocket Hijacking attacks"
  12. ^ "How Web Sockets Interact With Proxy Servers". Retrieved 2011-12-10. 
  13. ^ "WebSocket -76 is incompatible with HTTP reverse proxies". 2010-07-06. Retrieved 2011-12-10. 
  14. ^ "The WebSocket protocol, draft hybi-09". June 13, 2011. Retrieved June 15, 2011. 
  15. ^ "IANA Uniform Resource Identifer (URI) Schemes". 2011-11-14. Retrieved 2011-12-10. 
  16. ^ "draft-hixie-thewebsocketprotocol-76 - The WebSocket protocol". Retrieved 2011-12-10. 
  17. ^ "WebSockets Protocol Test Report". 2011-10-27. Retrieved 2011-12-10. 
  18. ^ "WebSockets (support in Firefox)". 2011-09-30. Retrieved 2011-12-10. 
  19. ^ "Bug 640003 - WebSockets - upgrade to ietf-06". Retrieved 2011-12-10. 
  20. ^ WebSockets - upgrade to ietf-07>
  21. ^ "Chromium bug 64470". 2010-11-25. Retrieved 2011-12-10. 
  22. ^ "WebSockets in Windows Consumer Preview". IE Engineering Team. 2012-03-19. Retrieved 2012-07-23. 
  23. ^ "WebKit Changeset 97247: WebSocket: Update WebSocket protocol to hybi-17". Retrieved 2011-12-10. 
  24. ^ "A hot Opera 12.50 summer-time snapshot". Opera Developer News. Archived from the original on 2012-08-05. Retrieved 2012-08-03. 
  25. ^ "WebSockets - MDN". 2011-09-30. Retrieved 2011-12-10. 
  26. ^ "List of Chromium Command Line Switches". Retrieved 2011-12-10. 
  27. ^ Wang, Vanessa; Salim, Frank; Moskovits, Peter (February 2013). "APPENDIX A: WebSocket Frame Inspection with Google Chrome Developer Tools". The Definitive Guide to HTML5 WebSocket. Apress.  

External links

This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.