Home System Design Tutorial What are Long Polling, WebSockets, and Server-Sent Events

What are Long Polling, WebSockets, and Server-Sent Events

Before understanding Long Polling, WebSockets, Server-Sent Events lets understand how the basic communication over the internet using HTTP request happens. HTTP is the most common method of data transfer in client server-based architecture.

This is how a regular HTTP request works –
  1. Whenever a client wants to visit a web page, it makes an HTTP request to a web server.
  2. The server calculates the response
  3. The server sends the response back to the client.

There are two ways for data transfer provided by HTTP protocol: HTTP Push and HTTP Pull. WebSockets, Long Polling, Server-Sent Events are push-based methods.

1. Long Polling

Polling technology allows the servers to push information to a client whenever the data is available instead of returning an empty response if no data is available(a.k.a Ajax Polling).

In long polling a client sends a request to the server and holds the connection open, thus allowing the server to respond whenever the data is ready. Once the data becomes available, a full response is sent to the client and the client then immediately re-request information from the server so that the server will almost always have an available waiting request.

HTTP Long Polling

HTTP Long Polling working
  1. A client sends a request for a webpage to a server using regular HTTP.
  2. If the response is available the server immediately sends it back but if data/response is not available the server holds the request until a data is available or a timeout has occurred.
  3. Once the client receives the requested webpage, it immediately sends another request to the server, re-starting the process.
  4. Each request has a timeout and the client has to reconnect if the connection is closed due to timeouts.

2. Server-Sent Events (SSE)

Server-Sent Events (SSE) is a server push technology that establishes a long persistent connection with the server, enabling a server to automatically send updates to a client(whenever available) via HTTP connection. They are commonly used to send message updates or continuous data streams to a browser client and designed to enhance native, cross-browser streaming. SSE uses Javascript API called EventSource to continuously update a client.

Server-Sent Events

Server-Sent Events Working
  1. The client creates a new JavaScript EventSource object and requests data from a server using regular HTTP.
  2. The server opens a connection between the client and the server.
  3. The server sends the data to the client whenever there’s new information available.
Application of SSE
  1. Live Twitter, Instagram news feed.
  2. Monitoring services like – Pingdom, uptime monitor, etc.
  3. News updates or alerts
  4. Live sports score or stock price chart.

3. WebSockets

WebSockets is a thin lightweight communication protocol that provides a full-duplex communication channel over a single TCP(transfer control protocol) connection. Just like HTTP protocol WebSockets too work at layer 5 of the OSI model and use layer 4 TCP, but they are different(HTTP is half-duplex).

WebSockets

WebSockets work at port number 443 and 80(in case of unsecured connection). The WebSocket protocol enables the communication between a web browser and a web server with low weight overheads, providing real-time data transfer from and to the server. This became possible by providing a standardized way for the server to send content to the browser without being asked by the client and allowing for messages to be passed back and forth while keeping the connection open.

LEAVE A REPLY

Please enter your comment!
Please enter your name here