Long Polling vs SSE vs WebSockets
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)));
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
45
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 11
ChatGPT 7
Perplexity 6
Unknown AI 3
Ahrefs 3
Google 3
SEMrush 2
Meta AI 1
How they use it
crawler 31
crawler_json 2
pre-tracking 3
Related categories
⚡
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