Sunday, January 31, 2016

What is HTML5 WebSocket?

If web pages use WebSockets protocol APIs than two-way communication with a remote host becomes possible. WebSockets interface supports full-duplex communication channel that operates through a single socket over the web.

HTML5 WebSocket reduces unnecessary web trafic and latency compared to (Ajax) polling and (Ajax) long-polling solutions used to simulate full-duplex connection by maintaining two connections.

Another nice feature of WebSocket is its ability to traverse firewalls and proxies. The communicaiton with a specific host and port are mediated by a tunneling connection to the proxy. WebSocket can detect the presence of proxy and all this happens automatically.

A HTTP connection switches to Websocket in a process called WebSocket handshake. The browser requests this handshake pointing to the WebSockets' site:

GET ws://echo.websocket.org/?encoding=text HTTP/1.1
Origin: http://websocket.org
Cookie: __utma=99as
Connection: Upgrade
Host: echo.websocket.org
Sec-WebSocket-Key: uRovscZjNol/umbTt5uKmw==
Upgrade: websocket
Sec-WebSocket-Version: 13


Here is a complete picture of the communication between web browser and an applicaiton through a WebSocket Server Gateway.

Read more here.

No comments: