What's behind "live" data updates? Understanding real-time technologies
\n\nHave you ever wondered how a chat works where messages arrive instantly, or a stock exchange ticker that updates by itself? Classic HTTP requests (where the browser knocks on the server and the server responds) don't work here — it's too slow and inefficient. You need the magic of real-time.
\n\nOn Stack Overflow, the question "What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?" has garnered thousands of views. This means confusion about these terms is normal. Today, we'll break everything down, write real PHP code, and understand when to use which technology.
\n\nWe won't use complex frameworks — just pure PHP and a couple of popular libraries. Let's go!
\n\n1. HTTP Polling — the grandfather of technologies
\n\nBefore talking about advanced things, let's recall basic polling. The client (JavaScript) simply sends a request to the server every N seconds. The server responds: "No new data" or "Here is the data."
\n\nPHP Code (server-side):
\n<?php\n// polling_server.php\nheader('Content-Type: application/json');\n\n// Simulate receiving data\n$data = ['message' => 'Hello at ' . date('H:i:s')];\necho json_encode($data);\n\n\nClient-side (JavaScript):
\nsetInterval(() => {\n fetch('polling_server.php')\n .then(response => response.json())\n .then(data => console.log(data.message));\n}, 3000); // Every 3 seconds\n\nPros: simplicity. Cons: lots of unnecessary requests, server load, delay of up to N seconds. For a chat, this is a deal-breaker.
\n\n2. Long-Polling — an elegant workaround
\n\nLong-Polling is an improved version of polling. The client opens an HTTP request, and the server does not respond immediately if there is no data. It keeps the connection open (hangs waiting). As soon as data appears — the server sends a response and closes the connection. The client immediately opens a new request.
\n\nFrom the client's perspective, it's a regular fetch. From the server's perspective, you need to be able to "sleep" and wake up.
\n\nLong-Polling Example in PHP
\n<?php\n// long_polling.php\nheader('Content-Type: application/json');\nheader('Cache-Control: no-cache');\n\n// Simulate waiting for new messages (e.g., from a file or database)\n$lastCheck = time();\n$timeout = 30; // wait a maximum of 30 seconds\n\nwhile (time() - $lastCheck < $timeout) {\n // Check if new data has appeared\n $newData = checkForNewMessages(); // your function\n if ($newData !== null) {\n echo json_encode(['status' => 'ok', 'data' => $newData]);\n exit;\n }\n // Sleep for 1 second to avoid CPU load\n sleep(1);\n}\n\n// If no data for 30 seconds — respond with an empty response\necho json_encode(['status' => 'timeout', 'data' => null]);\n\n\nImportant: In a real project, replace sleep(1) with usleep(500000) (0.5 sec) and use non-blocking checks (e.g., filemtime() for a file or SELECT ... FOR UPDATE with a timeout in the database).
Where it's used: old chats, notifications on websites without WebSockets. Downside: still many connections, the server spends resources on "hanging" requests.
\n\n3. Server-Sent Events (SSE) — one-way channel from the server
\n\nSSE (Server-Sent Events) is a technology built into HTML5. The server can send data to the client at any time, but only in one direction (server → client). The client is a regular EventSource in JavaScript.
\n\nSSE is ideal for: news feeds, stock tickers, logs, notifications.
\n\nPHP Code (server):
\n<?php\n// sse_server.php\nheader('Content-Type: text/event-stream');\nheader('Cache-Control: no-cache'