Wikipedia: “WebSocket is a web technology providing for multiplexing bi-directional, full-duplex communications channels over a single TCP connection”.


What is WebSocket? (http://www.websocket.org)

The WebSocket specification—developed as part of the HTML5 initiative—introduced the WebSocket JavaScript interface, which defines a full-duplex single socket connection over which messages can be sent between client and server. The WebSocket standard simplifies much of the complexity around bi-directional web communication and connection management.

WebSocket represents the next evolutionary step in web communication compared to Comet and Ajax. However, each technology has its own unique capabilities. Learn how these technologies vary so you can make the right choice.

WebSockets—like other pieces of the HTML5 effort such as Local Storage and Geolocation—was originally part of the HTML5 specification, but was moved to a separate standards document to keep the specification focused. WebSockets has been submitted to the Internet Engineering Task Force (IETF) by its creators, the Web Hypertext Application Technology Working Group (WHATWG). Authors, evangelists, and companies involved in the standardization still refer to the original set of features, including WebSockets, as “HTML5.”

Use cases:

  • Multiplayer online games
  • Chat applications
  • Live sports ticker
  • Realtime updating social streams

A small example, using JavaScript events to communicate with a WebSockets enabled server (wss://echo.websocket.org/):

<!DOCTYPE html>

<meta charset="utf-8" />

<title>WebSocket Test</title>

<script language="javascript" type="text/javascript">

var wsUri = "wss://echo.websocket.org/";//to avoid proxies
var output;

function init()
output = document.getElementById("output");

function testWebSocket()
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };

function onOpen(evt)
doSend("WebSocket rocks");

function onClose(evt)

function onMessage(evt)
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');

function onError(evt)
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);

function doSend(message)
writeToScreen("SENT: " + message);

function writeToScreen(message)
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;

window.addEventListener("load", init, false);


<h2>WebSocket Test</h2>

<div id="output"></div>





WebSocket on Wikipedia

About HTML5 WebSockets

Introducing WebSockets: Bringing Sockets to the Web

WebSockets API

WebSockets Echo Test

HTML5 Web Sockets: A Quantum Leap in Scalability for the Web

Welcome to Kaazing’s Documentation Center!



Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s

A %d blogueros les gusta esto: