AJAX - Long Polling and Server Push Techniques

Image

Image

 

 


1. Why Do We Need Long Polling or Server Push?

Traditional AJAX works like this:

  • Client sends a request

  • Server responds immediately

  • Connection closes

This is fine for static data, but not suitable for real-time updates like:

  • Chat messages

  • Notifications

  • Live scores

  • Order status updates

To handle near real-time data before WebSockets, developers used polling techniques.


2. What Is Polling? (Basic Idea)

Polling means:

The client repeatedly asks the server: “Any new data?”

There are two main types:

  1. Short Polling

  2. Long Polling


3. Short Polling (For Understanding)

In short polling:

  • Client sends requests at fixed intervals (e.g., every 5 seconds)

  • Server responds immediately (even if no data)

Example:

Client → Request → Server → “No update”
(wait 5 sec)
Client → Request → Server → “No update”

❌ Wastes bandwidth
❌ Server load increases
❌ Delayed updates


4. What Is Long Polling?

Long polling is an improved polling technique where:

  • Client sends a request

  • Server keeps the request open

  • Responds only when new data is available

  • Client immediately sends another request after receiving response

This creates an illusion of server push.


5. Long Polling Flow (Step by Step)

Step 1: Client sends AJAX request

fetch("/notifications");

Step 2: Server holds the request

  • No data yet

  • Connection stays open

Step 3: New data arrives on server

  • Server sends response immediately

Step 4: Client processes data and re-requests

  • Another long poll starts

Client ─────────────► Server (wait)
Client ◄──── Data ─── Server
Client ─────────────► Server (wait again)

6. Why Is It Called “Server Push”?

Although the client initiates the request:

  • The server decides when to respond

  • Data is sent instantly when available

Hence, it behaves like server push.


7. Key Characteristics of Long Polling

Feature Long Polling
Real-time feel Yes
HTTP-based Yes
Persistent connection Temporary
Bandwidth efficient More than short polling
Complexity Medium

8. Example Use Case (Easy to Visualize)

Chat Application

  • User A sends message

  • Server immediately responds to waiting long-poll request

  • User B receives message instantly

  • New long-poll request is started

This avoids constant “Are there messages?” requests.


9. Long Polling vs Short Polling (Exam Table)

Aspect Short Polling Long Polling
Request frequency Fixed intervals Event-based
Server load High Lower
Response delay Possible Minimal
Efficiency Low Higher

10. Limitations of Long Polling (Very Important)

❌ Each update still requires a new HTTP request
❌ Server resources held while waiting
❌ Not scalable for very large users
❌ Complex server-side logic

This is why modern apps prefer WebSockets or Server-Sent Events (SSE).


11. Long Polling vs Modern Server Push

Technique Connection True Push Usage
Long Polling Repeated HTTP No (simulated) Legacy
SSE Persistent Yes (one-way) Notifications
WebSockets Persistent Yes (two-way) Chats, games

12. Exam-Friendly One-Line Definitions

  • Long Polling:

    A technique where the server delays its response to an AJAX request until new data is available, enabling near real-time updates.

  • Server Push (Simulated):

    A method where the server sends data to the client as soon as it becomes available without continuous client polling.


13. Why Students Must Learn This

  • Common AJAX theory question

  • Helps understand real-time web evolution

  • Explains how apps worked before WebSockets

  • Appears in difference-based exam questions


14. Key Line to Remember for Exams

Long polling reduces unnecessary requests by holding the connection open until data is ready, unlike short polling which checks repeatedly.