Skip to main content

Polling Methods

Session initiation only from client possible

We need polling for one reason. Only the client can send a request to the web server and ask for data. The server can't open an HTTP connection to the client's IP to push data.

Long Polling

The server doesn't respond right away. It holds the HTTP session until a change occurs. Then it responds.

long-polling

Short Polling

It responds at once with whatever update it has.

short-polling

Server Sent Events

The client starts one HTTP session. The server uses that session to send many responses. This is a one-way path back to the client.

difference between standard HTTP request and SSE

The difference is in the Accept and Content-Type HTTP headers. The value should be text/event-stream. This tells both sides it's SSE based.

The responses also carry Transfer-Encoding: chunked. This means more responses can be expected.

sse-polling

Websockets

The client starts one HTTP session to do a handshake with the server. This opens a websocket session. After that, both sides talk using the ws protocol. This is a two-way path. They send many requests and responses over the same session.

Websocket use

Websockets are mostly used in chat based communication.

Websocket protocol blocked

Websockets use their own protocol. Firewalls and load balancers often block them. They need special setup to work.

websockets

keep-alive Connection Header vs Server-Sent Events

The Connection: keep-alive HTTP header decides how the TCP layer 4 connection is handled between server and client.

It means the layer-4 TCP connection stays alive even after the HTTP session closes.

Multi Node Clusters

With polling, remember that any node in a cluster may handle the request. That node must fetch data from a central system, such as a database.