WebSocket is an API built on top of TCP sockets and a protocol for bi-directional, full-duplex communication between client and server without the overhead of http.
WebSockets (or WebSocket) is an API and a protocol for bi-directional, full-duplex communication over TCP sockets. The WebSockets API was originally part of the HTML5 standard, but it has been split off into a separate W3C standard. The WebSockets protocol is an IETF standard described in RFC 6455.
The WebSockets API has full browser support in Chrome 14, Firefox 6, IE 10 (desktop and mobile), Opera 12.1 (desktop and mobile), Safari 6.0 (desktop and mobile), Android 4.4, Chrome Mobile, and Firefox Mobile. Some older browsers have partial support or can be supported using a Flash based fallback.
WebSockets supports both unencrypted and encrypted connections. Unencrypted connections use the "ws://" URL scheme and default to port 80. Encrypted connections use the "wss://" URL scheme and default to port 443. Encrypted connections use Transport Layer Security (TLS).
Simple WebSockets browser JavaScript example:
if ("WebSocket" in window) {
var ws = new WebSocket("ws://echo.websocket.org/");
ws.onopen = function() {
console.log("WebSockets connection opened");
ws.send("a test message");
}
ws.onmessage = function(e) {
console.log("Got WebSockets message: " + e.data);
}
ws.onclose = function() {
console.log("WebSockets connection closed");
}
} else {
// No native support
}
Useful Links
- Websockets echo server
- Chromium blog WebSockets announcement
- Socket.IO: a JavaScript client and server (Node.js) library that provides cross-browser communication with automatic fall-back to the best supported method.
- websockify: a WebSockets to generic TCP sockets proxy/bridge. Three implementations for reference: Python, C, and Node.js.
- WebSockets vs BOSH/Channel API
- WebSockets browser and server support: Stack Overflow answer, caniuse.com list
- WebSockets with Spring Framework 4.0