Kode Blog - Inspiring And Empowering Developers.
This tutorial serves as a quick introduction to HTML5 WebSockets. It also compares WebSockets to other similar technologies such as Comet and Ajax.
By the end of this tutorial, you should know what a WebSocket is, and how its different from other similar technologies such as Comet and Ajax.
HTML is the acronym for Hyper Text Mark-up Language. It is used to structure and format web pages. The number five (5) refers to revision number. HTML5 was developed to address the limitations of the earlier versions. Some of the limitations of the earlier versions of HTML include;
Since it is a relatively new technology, HTML5 is not supported in all the browsers. Earlier versions of most browsers do not support it. New versions of almost all browsers now support HTML5 either fully or partially. Most browsers on smart phones also support HTML5. The official logo for HTML5 is shown below.
The web has evolved from simple static text pages to complex interactive pages with rich multimedia content such as video. Web applications such as those for stock exchange need to update the client browser in real time. Application such as FaceBook chat needs to be able to send instant notifications when a user initiates a chat. How can all these requirements be met without turning to HTML5? The solution is comets.
A comet is a web programming technique that pushes data to the web browser without the web browser requesting for the data. The standard model of the web is the client requests for resources then the server responds with the requested resource.
Websocket is a low latency bi-directional communication (full duplex) protocol over a single TCP connection that supports over protocols such as HTTP, STOMP, and AMQP etc. It is a feature that was introduced in HTML5. It was developed to solve the disadvantages of technologies such as comet programming listed in the above section. WebSockets create persistence connections to the server that remain open until the client application is closed.
The WebSocket protocol as stated above is full duplex protocol over a single TCP connection. The protocol consists of an opening handshake that is interpreted by HTTP as an upgrade request followed by basic message framing, layered over TCP. The protocol uses origin-based security model. The aim of websocket protocol is to provide bi-directional communication between a client and a server without opening multiple connections. A WebSocket supports both UTF-8 strings and binary frames. The websocket protocol runs on port 80 for un-encrypted communication and port 443 for secure communication. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011
The websocket API is what allows web pages to interact with the WebSocket protocol using bi-directional communication with low latency.
Tutorial version 1: Date Published 2015-08-20