What are Long-Polling, Websockets, Server- Sent Events (SSE) and Comet?


Long-Polling, Websockets, Server-Sent Events (SSE), and Comet are some of the ways for the client-side to connect with the server-side in real-time. These are discussed in the next paragraphs.

Long Polling

This is a technique in which a client asks for information from a server but does not anticipate a response right away. It entails sending an HTTP request to a server and then leaving the connection open to allow the server to reply later. The server enables about 6 simultaneous connections from the browser using extended polling.

In comparison to other methods, load balancing is simple in this case. Long polling is the most traditional method, and it is supported by all web browsers. Due to the limited number of updates, it does not support reconnection. Long polling is more resource-intensive or demanding on the server, but it is more commonly supported by browsers.

Websockets

A WebSocket is a computer communication technology that allows us to have full-duplex communication channels over a single TCP connection.

The WebSocket protocol allows for low-weight interaction between a web browser and a web server, allowing for real-time data flow from and to the server. This is accomplished by establishing a common method for the server to transmit information to the client without first receiving a request from the client, and then enabling messages to be transferred back and forth while the connection remains open. In this approach, the client and the server may have a two-way progressing dialogue without any problems.

Websockets are widely supported by browsers including Google Chrome, Opera, Edge, Firefox, Safari, and others. WebSockets is a lightweight browser extension that allows for up to 1024 simultaneous connections. It uses a convoluted load balancing and proxy system. It also allows lost client detection, which was previously unavailable with extended polling, although it does not support reconnection.

Server-Sent Events (SSE)

This is a technique that allows a browser to automatically receive updates from any server over an HTTP connection.

The WHATWG (web hypertext application technology working group) proposed this concept, and the Opera web browser was the first to adopt it in 2006. It's a specification that explains how servers initiate data transfer with clients after establishing a client connection. They use a JavaScript API called EventSource to send message updates or continuous updates to a client to improve cross-browser streaming.

Only a few browsers, including Mozilla, Chrome, and Safari, support SSE. This method is still not supported by Internet Explorer and Edge. It also allows for up to six simultaneous connections from the browser. It allows for simple load balancing and EventSource-supported reconnection management.

Comet

This is a web application paradigm in which the server uses a long-held HTTPS request to push data to a client-server, i.e. a web browser, without the web browser actively asking for any data updates. Comet is also known by a variety of other names, including Ajax Push, Reverse Ajax, and so on. The core concept behind Comet was to perform a single, normal HTTPS request and rely on an infinite response.

The web server accepts new incoming requests and starts a new response with the current data, but the server does not close the response stream, so the browser keeps the connection open and waits for more data. At the conclusion of each update, the server delivers a unique string. For example, "ThisCometMessageEnded."

By providing two-way communication, Comet eliminates the constraints of the page-by-page web paradigm and polling.

Updated on: 28-Oct-2021

467 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements