Skip to content

Core\Sse::startStream

Core\Sse::startStream(): Core\Sse

Starts an event stream as the response to the current request, and returns a Core\Sse object to send events on.

An event stream is a response that stays open. The server sends events one at a time, and the client receives each event as soon as it is sent. The server sends the response headers as soon as you call startStream(). The stream ends when the request ends. For events that continue after the request, use Core\Sse::upgrade.

An event stream always has the status 200, and you cannot change it. If the program calls Core\Response::setStatus before startStream(), then startStream() throws a LogicError.

The examples below send two events, show the error for a status that was set first, and report each step of a deployment as it finishes.

Return value

Returns Core\Sse. A Core\Sse to send events on. It is the same class that upgrade uses, so a helper function works with both. A browser's EventSource reconnects when this stream ends. Use this stream with fetch, or with a progress display that closes itself.

Errors

  • LogicErrorA status was set on this request before the stream was opened. An event stream always has status 200. It is also thrown when this response is already streaming a body, because a response has only one body.

Changelog

VersionChange
0.0.6Core\Sse::startStream starts an event stream, and setRetryDelay sets how long a browser waits before it connects again. The old names were stream and retry.

Examples

Sending two events

<?nvs
// Shows how to start an event stream and send two events on it.
// `startStream()` starts the event stream and returns the object that sends events.
Core\Sse $sse = Core\Sse::startStream();
// Each `send()` writes one event. This prints `data: Loading` and then `data: Ready`.
$sse->send('Loading');
$sse->send('Ready');
Output
data: Loading
data: Ready

A status set first

<?nvs
// Shows the error you get when the program sets a status before it starts an event stream.
// An event stream always has the status `200`, so a program cannot set its own.
Core\Response::setStatus(202);
try {
Core\Sse::startStream();
} catch (LogicError $e) {
// This prints the error message. No stream was started.
echo $e->message, "\n";
}
Output
Core\Sse::startStream(): this request set the status `202` and an event stream is `200` by protocol, so the two cannot both be answered

Reporting each deploy step

<?nvs
// Shows a deployment that reports each step to the browser as soon as the step is done.
array<string> $steps = ['pull the new code', 'install packages', 'run the migrations', 'restart the workers'];
Core\Sse $sse = Core\Sse::startStream();
uint $done = 0;
foreach ($steps as string $step) {
// A real deployment would run the step here.
$done = $done + 1;
// The browser shows each `step` event as soon as it arrives.
$sse->send($done . '. ' . $step, event: 'step');
}
// The last event tells the page that the deployment is finished.
$sse->send(['steps' => $done, 'status' => 'finished'], event: 'done');
Output
event: step
data: 1. pull the new code
event: step
data: 2. install packages
event: step
data: 3. run the migrations
event: step
data: 4. restart the workers
event: done
data: {"steps":4,"status":"finished"}