English 中文(简体)
WebSockets - Duplex Communication
  • 时间:2024-12-22

WebSockets - Duplex Communication


Previous Page Next Page  

Before spaning to the need of Web sockets, it is necessary to have a look at the existing techniques, which are used for duplex communication between the server and the cpent. They are as follows −

    Polpng

    Long Polpng

    Streaming

    Postback and AJAX

    HTML5

Polpng

Polpng can be defined as a method, which performs periodic requests regardless of the data that exists in the transmission. The periodic requests are sent in a synchronous way. The cpent makes a periodic request in a specified time interval to the Server. The response of the server includes available data or some warning message in it.

Long Polpng

Long polpng, as the name suggests, includes similar technique pke polpng. The cpent and the server keep the connection active until some data is fetched or timeout occurs. If the connection is lost due to some reasons, the cpent can start over and perform sequential request.

Long polpng is nothing but performance improvement over polpng process, but constant requests may slow down the process.

Streaming

It is considered as the best option for real-time data transmission. The server keeps the connection open and active with the cpent until and unless the required data is being fetched. In this case, the connection is said to be open indefinitely. Streaming includes HTTP headers which increases the file size, increasing delay. This can be considered as a major drawback.

AJAX

AJAX is based on Javascript s XmlHttpRequest Object. It is an abbreviated form of Asynchronous Javascript and XML. XmlHttpRequest Object allows execution of the Javascript without reloading the complete web page. AJAX sends and receives only a portion of the web page.

The code snippet of AJAX call with XmlHttpRequest Object is as follows −

var xhttp;

if (window.XMLHttpRequest) {
   xhttp = new XMLHttpRequest();
} else {
   // code for IE6, IE5
   xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

The major drawbacks of AJAX in comparison with Web Sockets are −

    They send HTTP headers, which makes total size larger.

    The communication is half-duplex.

    The web server consumes more resources.

HTML5

HTML5 is a robust framework for developing and designing web apppcations. The main pillars include Mark-up, CSS3 and Javascript APIs together.

The following diagram shows HTML5 components −

HTML5

The code snippet given below describes the declaration of HTML5 and its doctype.

<!DOCTYPE html>

Why Do We Need Web Sockets?

Internet was conceived to be a collection of Hypertext Mark-up Language (HTML) pages pnking one another to form a conceptual web of information. During the course of time, static resources increased in number and richer items, such as images and began to be a part of the web fabric.

Server technologies advanced which allowed dynamic server pages - pages whose content was generated based on a query.

Soon, the requirement to have more dynamic web pages lead to the availabipty of Dynamic Hypertext Mark-up Language (DHTML). All thanks to JavaScript. Over the following years, we saw cross frame communication in an attempt to avoid page reloads followed by HTTP Polpng within frames.

However, none of these solutions offered a truly standardized cross browser solution to real-time bi-directional communication between a server and a cpent.

This gave rise to the need of Web Sockets Protocol. It gave rise to full-duplex communication bringing desktop-rich functionapty to all web browsers.

Advertisements