← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Long Polling & Server-Sent Events (SSE)

Architecture PHP 5.0+ Intermediate
debt(d7/e5/b5/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints indicate no automated detection is available — the code pattern (JavaScript polling every second with Ajax, PHP holding connections open) requires a human reviewer to recognize the architectural anti-pattern, or profiling under load to see connection exhaustion. No tools in detection_hints.tools catch this automatically.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix points to replacing long polling with SSE, but this is not a one-line swap — it requires changing both the server-side PHP to emit SSE headers and the client-side JavaScript reconnect loop, plus addressing timeout handling and duplicate-event tracking across both layers. Multiple files and paradigm shift within the realtime component.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). Long polling applies to web contexts and any realtime feature built on it carries ongoing maintenance cost: every developer touching the connection management, timeout logic, reconnect loop, and deduplication pays a tax. It doesn't define the entire system's shape (not b7) but it persistently complicates any feature that uses it.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field explicitly states that developers treat long polling and WebSockets as equivalent for real-time data, not realizing each long-poll message incurs full connection overhead and requires client-side reconnection. Common mistakes reinforce this — forgetting server-side timeouts and the reconnect loop are well-known but frequently hit pitfalls.

About DEBT scoring →

Also Known As

long poll HTTP long polling Comet

TL;DR

Server-side push techniques for PHP — long polling holds HTTP connections open until data is ready; SSE streams events over a persistent HTTP response.

Explanation

Long polling: the client makes a request, the server holds it open (sleeping/waiting) until new data is available or a timeout occurs, then responds and the client immediately reconnects. Simple to implement in PHP but holds a PHP-FPM worker for the duration — inefficient under load. Server-Sent Events (SSE): the server sends a text/event-stream response and pushes data: payloads down over a persistent connection using ob_flush() and flush(). SSE is one-directional (server to client) and reconnects automatically. Both approaches consume a PHP worker per open connection — for high concurrency, a Node.js/Go push service with Redis Pub/Sub as the backend is more scalable.

Diagram

sequenceDiagram
    participant C as Client
    participant S as Server
    Note over C,S: Long Polling
    C->>S: GET /events (holds connection)
    Note over S: Waits for event...
    S-->>C: Response when event ready
    C->>S: Immediately reconnect
    Note over C,S: Server-Sent Events (SSE)
    C->>S: GET /stream (persistent)
    S-->>C: data: event1
    S-->>C: data: event2
    Note over C,S: WebSocket - full duplex
    C->>S: WS upgrade
    S-->>C: push anytime
    C->>S: send anytime

Common Misconception

Long polling and WebSockets are equivalent for real-time data. Long polling holds an HTTP connection open until data is available then the client reconnects — each message incurs connection overhead. WebSockets maintain a persistent bidirectional connection with far lower per-message latency.

Why It Matters

Long polling holds an HTTP connection open until data is available, giving near-real-time updates without WebSockets — but ties up server connections and requires careful timeout handling.

Common Mistakes

  • Not setting a server-side timeout — a connection held indefinitely exhausts worker threads.
  • Not handling the client reconnect loop — when the server responds, the client must immediately re-poll.
  • Using long polling when WebSockets or SSE are available and more efficient for the use case.
  • Not tracking which clients are waiting and what data they have already seen — sending duplicate events.

Code Examples

✗ Vulnerable
// Long poll without timeout — holds worker indefinitely:
set_time_limit(0); // Dangerous — worker never freed
while (true) {
    $events = getNewEvents($lastId);
    if (!empty($events)) { echo json_encode($events); exit; }
    sleep(1);
}
// Fix: set_time_limit(30); respond with empty after timeout so client reconnects
✓ Fixed
// SSE in PHP
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
while (true) {
    echo 'data: ' . json_encode(getUpdate()) . "\n\n";
    ob_flush(); flush();
    sleep(1);
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 52
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 2 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 7 Perplexity 6 ChatGPT 5 Scrapy 5 Ahrefs 4 SEMrush 4 Google 3 Unknown AI 2 Claude 2 Majestic 1 Meta AI 1 Bing 1 PetalBot 1
crawler 37 crawler_json 5
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Medium
⚡ Quick Fix
Use Server-Sent Events instead of long polling for PHP — SSE is simpler, uses one connection, and auto-reconnects; long polling creates a new HTTP connection for every update which wastes resources
📦 Applies To
PHP 5.0+ web
🔗 Prerequisites
🔍 Detection Hints
JavaScript polling every second with Ajax; PHP holding connection open without SSE; repeated short-interval fetch calls instead of push
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File Tests: Update


✓ schema.org compliant