What is Long polling, how does it work, and why would you use it? Considering server and client resources, what is the main drawback of using long polling? Which HTML5 feature is the best alternative to long polling?

The HTTP protocol uses a request/response model, which implies the server cannot provide data to the client (i.e., the server can only provide data to the client in response to a client request). Long polling is a web application development style that simulates data being pushed from the server to the client. When the client uses the lengthy polling pattern, the client sends a request to the server, and the connection is kept open until the server is ready to provide data to the client. Only when data has been delivered back to the client or a connection timeout has occurred is the connection terminated. When the connection is closed, the client creates a new request, repeating the loop.

When utilizing lengthy polling, there are two significant disadvantages to consider:

  1. Long polling queries are treated the same as any other HTTP request, and web servers respond accordingly. This means that each long poll connection will use up server resources, potentially limiting the server’s connection capacity. HTTP connection timeouts may result as a result of this.

  2. The maximum number of connections a web application may make is limited by each web browser. This implies that the load time and performance of your application may suffer as a result.

A WebSocket is a helpful alternative to lengthy polling in HTML5. A WebSocket is a technology that allows users to communicate in full-duplex over a single TCP connection. The WebSocket protocol allows for more interaction between a browser and a website, allowing for live content and removing the need for long polling cycles.

Server-sent DOM Events might be another solution. Rather than constantly requesting data from a server, this technique sends data from the server to the browser in a continuous stream. However, Microsoft Internet Explorer does not support this HTML5 functionality, making it a less appealing option.