How to Build Real-Time SSE and HTTP Chunk Streams in Webman (PHP)
This guide explains the concept of stream output, its advantages and typical scenarios, then provides step‑by‑step implementations of Server‑Sent Events and HTTP chunk streaming using the Webman framework, including PHP process code, configuration, startup commands, and front‑end testing.
Concept
Stream output is a data‑processing approach that transmits and handles data as a continuous flow rather than storing it centrally, enabling real‑time processing and higher efficiency.
Advantages of Stream Output
Real‑time : Reduces latency by processing data as it arrives.
Efficiency : Parallel handling of distributed data avoids redundant transmission.
Scalability : Adding nodes expands processing capacity.
Flexibility : Supports structured, semi‑structured, and unstructured data.
Typical Use Cases
Real‑time analytics : e.g., stock market trend prediction.
Recommendation systems : personalize content based on live user behavior.
Monitoring systems : track network traffic, device status, etc.
Speech/NLP : live transcription, machine translation.
IoT data processing : analyze sensor streams instantly.
Implementation with Webman
Webman SSE
SSE (Server‑Sent Events) keeps an HTTP connection open after the client sends a request with Accept: text/event-stream , allowing the server to push data continuously.
Documentation: https://www.workerman.net/doc/workerman/http/SSE.html Process file:
process/EventStreamProcess.php <?php
/**
* @desc EventStreamProcess.php
*/
declare(strict_types=1);
namespace process;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\ServerSentEvents;
use Workerman\Protocols\Http\Response;
use Workerman\Timer;
class EventStreamProcess
{
public function onMessage(TcpConnection $connection, Request $request)
{
if ($request->header('accept') === 'text/event-stream') {
$connection->send(new Response(200, [
'Content-Type' => 'text/event-stream',
'Access-Control-Allow-Origin' => '*'
]));
$timerId = Timer::add(2, function () use ($connection, &$timerId) {
if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
Timer::del($timerId);
return;
}
$connection->send(new ServerSentEvents([
'event' => 'message',
'data' => '开源技术小栈 '.date('Y-m-d H:i:s'),
'id' => time()
]));
});
return;
}
$connection->send('ok');
}
}Configuration entry (add to config/process.php):
return [
// ... other configurations ...
'event-stream' => [
'listen' => 'http://0.0.0.0:8288',
'handler' => \process\EventStreamProcess::class,
]
];Start Webman
php start.php start
# Output shows workers and the event‑stream listener on port 8288Front‑end test code
<!DOCTYPE html>
<html>
<head><title>SSE Demo</title></head>
<body>
<h2>使用webman创建text/eventstream响应</h2>
<script>
var source = new EventSource('http://127.0.0.1:8288');
source.addEventListener('message', function (event) {
console.log(event.data); // e.g., 开源技术小栈 2024-10-31 21:40:57
}, false);
</script>
</body>
</html>Result screenshots:
Webman HTTP Chunk
Process file:
process/HttpChunkStreamProcess.php <?php
/**
* @desc webman Http Chunk solution
*/
declare(strict_types=1);
namespace process;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Chunk;
use Workerman\Protocols\Http\Response;
use Workerman\Timer;
class HttpChunkStreamProcess
{
public function onMessage(TcpConnection $connection, Request $request)
{
$total_count = 10;
$connection->send(new Response(200, [
'Transfer-Encoding' => 'chunked',
'Access-Control-Allow-Origin' => '*'
], "开源技术小栈。共【{$total_count}】段数据<br>"));
$timer_id = Timer::add(2, function () use ($connection, &$timer_id, $total_count) {
static $count = 0;
if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
Timer::del($timer_id);
return;
}
if ($count++ >= $total_count) {
$connection->send(new Chunk(''));
return;
}
$connection->send(new Chunk("开源技术小栈。第【{$count}】段数据<br>"));
});
}
}Add to config/process.php:
return [
// ... other configurations ...
'event-stream' => [
'listen' => 'http://0.0.0.0:8289',
'handler' => \process\HttpChunkStreamProcess::class,
]
];Visit http://127.0.0.1:8289 in a browser – the page will output chunked data at regular intervals.
Screenshot:
Related documentation:
https://www.workerman.net/doc/workerman/http/response.html#%E5%8F%91%E9%80%81http%20chunk%E6%95%B0%E6%8D%AEWebman Push Plugin
Official plugin documentation: https://www.workerman.net/plugin/2
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
