Core\Sse::current
Returns the event stream your program is writing to.
An event stream is opened in one of two ways. A request calls Core\Sse::startStream to answer with events, or Core\Sse::upgrade to start a script that keeps sending after the request ends. Code that did not open the stream calls current() to get it, and then calls send() on it. Every call returns an object for the same stream, so events from different places arrive in the order they were sent.
A program that has not opened a stream has none. In that case current() throws a LogicError. This is also true for a response that writes an ordinary body over time with Core\Response::startStream.
The examples below send an event from a helper, show the error when no stream is open, and report the progress of an import job.
Return value
Returns Core\Sse. The same Core\Sse that upgrade or startStream returned. A helper function that takes one works with both kinds of stream.
Errors
LogicErrorThe program is not writing an event stream. This is true for every command-line program and everyspawn scriptchild. It is also true for a request with an ordinary body. A streamed response body is not an event stream either.
Examples
Sending from a helper
<?nvs// Shows a helper that sends an event on a stream it did not open itself.
class Notice{ public static function send(string $text): void { // `Core\Sse::current()` returns the event stream this program is writing. Core\Sse::current()->send($text, event: 'notice'); }}
// `Core\Sse::startStream()` opens the stream. Every event prints as the client receives it.Core\Sse::startStream();// This prints `event: notice` and then `data: your order has shipped`.Notice::send('your order has shipped');event: noticedata: your order has shippedOutside an event stream
<?nvs// Shows what `current()` does in a program that has not opened an event stream.
// Nothing opened a stream here, so `current()` throws a `LogicError`.// This prints `no event stream here`.try { Core\Sse $sse = Core\Sse::current(); echo "stream found\n";} catch (LogicError $e) { echo "no event stream here\n";}no event stream hereReporting import progress
<?nvs// Shows an import job that reports its progress to the browser while it works.
class Import{ public static function run(array<string> $emails): uint { var $total = Core\Arr::count($emails); uint $done = 0; foreach ($emails as string $email) { // A real job would save the customer here. $done = $done + 1; // The job gets the stream itself, so the caller does not pass it in. Core\Sse::current()->send(['done' => $done, 'total' => $total], event: 'progress'); } return $done; }}
// The request opens the stream, and then starts the job.Core\Sse $sse = Core\Sse::startStream();uint $imported = Import::run(['ana@example.com', 'bo@example.com', 'chen@example.com']);// This prints three `progress` events and then `data: imported 3 customers`.$sse->send('imported ' . $imported . ' customers');event: progressdata: {"done":1,"total":3}
event: progressdata: {"done":2,"total":3}
event: progressdata: {"done":3,"total":3}
data: imported 3 customers