Websockets.org

Kaazing.com


Test Service Locations. Websocket Echo.

Coming Soon

 

Notes:
A WebSocket test server will be made publicly available.
Developers will be able to test compliance against simple sockets.

Service-sent Event Timestamp

Coming Soon

 

Notes:
A timestamp service for testing compliance will soon be available.


Is your current browser Websocket compatible?


Coming Soon

HTML5 WebSockets

The HTML5 initiative introduced the Web Socket interface -- now developed as an independent specification -- which defines a full-duplex communications channel that operates over a single socket and is exposed via a JavaScript interface in compliant browsers. The bi-directional capabilities of Comet and Ajax, unlike Web Sockets, are not native to the browser, and rely on maintaining two connections-one for upstream and one for downstream--in order to stream data to and from the browser. In addition, few existing solutions support streaming over HTTP, instead employing a technique called "long-polling" with higher overhead.

Web Sockets account for network hazards such as proxies and firewalls, making streaming possible over any connection, and with the ability to support upstream and downstream communications over a single connection, Web Sockets place less burden on your servers, allowing existing machines to support more than twice the number of concurrent connections.

Simple is Better

With the introduction of one succinct interface (see Listing 1), developers can replace techniques such as long-polling and "forever frames," and as a result further reduce latency.

[Constructor(in DOMString url, optional in DOMString protocol)]
interface WebSocket {
   readonly attribute DOMString URL;
   // ready state
   const unsigned short CONNECTING = 0;
   const unsigned short OPEN = 1;
   const unsigned short CLOSED = 2;
   readonly attribute unsigned short readyState;
   readonly attribute unsigned long bufferedAmount;

   // networking
   attribute Function onopen;
   attribute Function onmessage;
   attribute Function onclose;
   boolean send(in DOMString data);
   void close();
};

Utilizing the Web Socket interface couldn't be simpler. To connect to an end-point, just create a new Web Socket instance, providing the new object with a URL that represents the end-point to which you wish to connect (See listing 2). Note that a ws:// and wss:// prefix are proposed to indicate a Web Socket and a secure Web Socket connection, respectively. A Web Socket connection is established by upgrading from the HTTP protocol to the Web Socket protocol during the initial handshake between the client and the server, over the same underlying TCP/IP connection. Once established, Web Socket data frames can be sent back and forth between the client and the server in full-duplex mode. The connection itself is exposed via the "onmessage" and "send" functions defined by the Web Socket interface.

var myWebSocket = new WebSocket("ws://www.websocket.org");

Before connecting to an end-point and sending a message, you can associate a series of event listeners to handle each phase of the connection life-cycle.

myWebSocket.onopen = function(evt) { alert("Connection open ..."); };
myWebSocket.onmessage = function(evt) { alert( "Received Message: "  +  evt.data); };
myWebSocket.onclose = function(evt) { alert("Connection closed."); };

To send a message to the server, simply call "send" and provide the content you wish to deliver. After sending the message, call "close" to terminate the connection. As you can see, it really couldn't be much easier.

myWebSocket.send("Hello Web Socket!");
myWebSocket.close();

Firewalls and Proxies? No Problem

One of the more unique features Web Socket provides is its ability to traverse firewalls and proxies, a problem area for many applications. Comet-style applications typically employ long-polling as a rudimentary line of defense against firewalls and proxies. The technique is effective, but is not well suited for applications that have sub-500 millisecond latency or high throughput requirements. Plugin-based technologies such as Adobe Flash, also provide some level of socket support, but have long been burdened with the very proxy and firewall traversal problems that Web Sockets now resolve.

A Web Socket detects the presence of a proxy server and automatically sets up a tunnel to pass through the proxy. The tunnel is established by issuing an HTTP CONNECT statement to the proxy server, which requests for the proxy server to open a TCP/IP connection to a specific host and port. Once the tunnel is set up, communication can flow unimpeded through the proxy. Since HTTP/S works in a similar fashion, secure Web Sockets over SSL can leverage the same HTTP CONNECT technique. Note that Web Sockets are just beginning to be supported by modern browsers. However, backwards compatible implementations that enable today's browsers to take advantage of this emerging technology are available.

Any RIA, Any Time

In addition, the Web Socket protocol can be used to support a diverse set of clients (e.g. JavaScript, Adobe Flex, JavaFX, Microsoft Silverlight, etc.). However, the HTML5 specification only defines support for JavaScript, which is limited to text-based protocols. To serve other client-types and support binary protocols you will need to look to external offerings.

Learn more about the Web Sockets API.