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

Long Polling vs SSE vs WebSockets

Networking PHP 5.0+ Intermediate
debt(d7/e3/b3/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 confirm automated detection is 'no', with only a code_pattern hint (JavaScript polling every second with Ajax). There is no tool in detection_hints.tools that catches this — identifying the wrong real-time mechanism (e.g. polling instead of SSE, or WebSocket instead of SSE) requires a developer to recognize the pattern in review or notice inefficiency in runtime testing.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix says to swap to SSE for server-to-client push, which is a clear replacement pattern. However, it involves replacing the client-side polling logic and server-side response format, touching both ends but within a localized component — more than a one-liner but not a multi-file architectural change.

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

Closest to 'localised tax' (b3). The applies_to scope is web contexts only, and the choice affects the real-time communication layer of a single feature or component. Picking the wrong mechanism (e.g. WebSocket when SSE suffices) adds complexity to that component but doesn't propagate a structural burden across the rest of the codebase.

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

Closest to 'notable trap' (t5). The misconception field states developers believe 'WebSockets are always better than SSE,' which is a documented gotcha — SSE has proxy/firewall compatibility and auto-reconnection advantages that most developers learn only after hitting production issues. It's a well-known trap but not one that contradicts a similar concept in another ecosystem, so t5 is appropriate.

About DEBT scoring →

Also Known As

SSE WebSocket long polling server-sent events real-time

TL;DR

Long polling (HTTP hacks), SSE (one-way push over HTTP), WebSockets (full-duplex) — use SSE for server-to-client only, WebSocket for bidirectional.

Explanation

Long polling: client holds HTTP request open, server responds when data is available, client reconnects immediately. Simple but high latency from reconnect overhead. SSE: browser opens persistent connection, server pushes text events with built-in reconnection and event IDs. One-directional, HTTP-compatible. WebSocket: full-duplex TCP after HTTP upgrade — bidirectional, binary and text, low latency. PHP: SSE with chunked transfer encoding + flush(). WebSockets via ReactPHP or Ratchet.

Common Misconception

WebSockets are always better than SSE — SSE uses standard HTTP, works through firewalls and proxies that block WebSocket upgrades, has automatic reconnection; use WebSocket only when bidirectional communication is genuinely needed.

Why It Matters

A live dashboard only receiving server updates uses SSE — simpler, better proxy compatibility, and automatic reconnection built-in vs WebSocket upgrade complexity.

Common Mistakes

  • Long polling with short timeouts — defeats the purpose
  • SSE without event IDs — client misses events during disconnection
  • WebSocket when SSE suffices — added complexity for no benefit
  • No heartbeat — proxies close idle connections after 30-90 seconds

Code Examples

✗ Vulnerable
// 1 request per second per user — expensive:
setInterval(() => {
    fetch('/api/updates').then(r => r.json()).then(update);
}, 1000);
✓ Fixed
// SSE — persistent, automatic reconnection:
// PHP:
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('X-Accel-Buffering: no'); // Disable nginx buffering
while (true) {
    echo "id: {$id}\ndata: " . json_encode(getLatest()) . "\n\n";
    ob_flush(); flush();
    if (connection_aborted()) break;
    sleep(1);
}

// JavaScript:
const es = new EventSource('/api/stream');
es.addEventListener('update', e => update(JSON.parse(e.data)));

Added 16 Mar 2026
Edited 22 Mar 2026
Views 84
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 1 ping T 2 pings F 2 pings S 2 pings S 1 ping M 0 pings T 1 ping W 3 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 2 pings T 1 ping W
Claude 1
PetalBot 2
Amazonbot 13 ChatGPT 7 Scrapy 7 Perplexity 6 Ahrefs 5 Google 5 SEMrush 5 Unknown AI 3 Twitter/X 3 Meta AI 2 Claude 2 Bing 2 PetalBot 2
crawler 55 crawler_json 4 pre-tracking 3
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Medium
⚡ Quick Fix
Use Server-Sent Events (SSE) for server-to-client one-way push — simpler than WebSockets, works through proxies, auto-reconnects; use long polling as a fallback for environments that block SSE
📦 Applies To
PHP 5.0+ web
🔗 Prerequisites
🔍 Detection Hints
JavaScript polling every second with Ajax when SSE would be more efficient; real-time updates not needing bidirectional communication
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File Tests: Update


✓ schema.org compliant