What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?
There are different ways for client-side to interact with the server-side in real time, i.e., Long-Polling, Websockets, Server-Sent Events (SSE) and Comet. These are explained as following below.
1. Long Polling:
It is a technology where the client requests information from the server without expecting an immediate response or basically involves making an HTTP request to a server and then holding the connection open to allow the server to respond later. Using long polling the server allows approximately 6 parallel connections from the browser.
Load balancing in this is easy compared to other ways. Long polling is the oldest ways and hence is supported on all web browsers. Though due to the fewer updates in this it does not provide re-connection handling. Long polling is a lot more intensive or heavy on the server, but more widely accepted for browsers.
WebSocket is a computer communication protocol that enables us full-duplex communication channels over a single transfer control protocol (TCP) connection. The WebSocket protocol enables interaction between a web browser and a web server with low weight overheads, providing real-time data transfer from and to the server. This is done by defining a standard way for the server to send information to the client without being first requested by the client, and then allowing messages to be passed to and fro while keeping the connection open. In this way, a two-way advancing conversation can take place between the client and the server without any issue.
Websockets are majorly accepted in web browsers such as google chrome, opera, edge, firefox, safari etc. WebSockets is light on the browser and it provides up to 1024 parallel connections from the browser. It has a complicated load balancing and proxying technique. It also supports dropped client detection which was absent in long polling but it also does not provide re-connection handling.
3. Server-Sent Events (SSE):
SSE is supported by a few browsers such as Mozilla, Chrome, and Safari. Internet Explorer and Edge still don’t support this technique. It also supports up to 6 parallel connections from the browser. It supports easy load balancing and also provides reconnection handling supported by EventSource.
It is a web application model in which a long-held HTTPS request allows the server to push data to a client-server i.e. a web browser without the web browser explicitly requesting any data update. Comet is known as many other names such as Ajax Push, Reverse Ajax etc. The basic idea behind developing Comet was to make a single and regular HTTPS request and depend on a never ending response from the server.
The web server takes new incoming request and starts a new response with the current data, but the server will not end the response and hence the browser keeps the connection open and waiting for new data, whenever new data arrives the server will write that to the response stream. The server sends a unique string at the end of that particular update. Eg. “ThisCometMessageEnded”.
Comet ends the limitations of the page by page web model and polling by offering two-way communication.
Specific methods of implementing Comet fall into two major categories: streaming and long polling.
- I. Streaming –
Any application that uses streaming Comet opens a single constant connection from the client browser to the server for all the Comet events. Techniques for streaming Comet are-
(a) Hidden iframe (b) XMLHttpRequest
- II. Long Polling –
Specific technologies for accomplishing long-polling include:
(a) XMLHttpRequest long polling (b) Script tag long polling