Streaming: Server-Sent Events

SSE is one-way push over plain HTTP — the browser opens an EventSource, the server keeps the response open and emits named events. $response->sse($fn) sets the right headers and gives you an $emit($data, $event, $id) callback. Below: Connect opens an EventSource to /stream/events; the event log fills with one tick per second.

How it's wired (route side)

$app->route('/stream/events', function ($response) {
    $response->sse(function ($emit) {
        $emit(json_encode(['status' => 'connected']), 'open');
        for ($i = 1; $i <= 10; $i++) {
            co::sleep(1);
            $emit(json_encode(['tick' => $i]), 'tick', (string)$i);
        }
        $emit(json_encode(['done' => true]), 'done');
    });
});

Connect

Click Connect. One event per second, 10 ticks then auto-closes.