Skip to content

Core\Sse::upgrade

Core\Sse::upgrade(
    string $entry,
    mixed $arguments = null,
    mixed $limits = null,
    ?array<string> $grants = null,
    ?string $on = null,
): void

Turns a request into an event stream that stays open after the request ends. The server can then send the client new events at any time, for example each time something is published on a topic.

You name what the stream runs: the path of a file, or a static method written Feed::run. The stream starts when the request ends. It runs in its own isolate (a separate copy of the program that shares no memory with the request). arguments: copies values into it. Inside the stream, Core\Sse::current() returns the stream to send on.

The other options are the ones spawn script takes. limits: sets lower limits, such as { memory: "8M" }. grants: lists the capabilities the stream may use, and it gets only the ones the request also has. on: can only be "here", so on: "worker" does not compile.

A program run from the command line has no request, so the call throws a RuntimeError. A second call in the same request also throws one.

The examples below open a stream that runs a file, show the error for a second call, and open a stream of notifications only for a signed-in user.

Parameters

  • $entrystringWhat the stream runs. It is a file path, or a static method written Feed::run. A file path is resolved and checked the same way as for spawn script. It cannot be an anonymous function. An anonymous function can capture variables, and an isolate shares only compiled code.
  • $argumentsmixeddefault nullThe values the stream starts with. Each one goes to the entry's parameter of the same name. They are deep-copied into the isolate, so they are never shared references. A secret value is not allowed. A tainted value stays tainted in the isolate.
  • $limitsmixeddefault nullLower limits for the stream, with the same syntax as for spawn script: { memory: "8M", cpu_time: "2s" }. The keys are memory, cpu_time, wall_time, max_output and max_tasks. A limit higher than the server's own has no effect. The stream stops when it reaches one.
  • $grants?array<string>default nullThe capabilities the stream may use, such as ["fs.read"]. It gets only the names in this list that the request itself has. An empty list gives it none. Without this option, it gets the capabilities of the request.
  • $on?stringdefault nullWhere the stream runs. Only "here" is allowed, because the stream runs on the core that accepted the client. "worker" does not compile.

Return value

Returns void. Nothing. The call itself opens the stream. A handler does not return it, because the return value of a handler is not used.

Errors

  • RuntimeErrorThe program is not handling a request. This is true for every command-line program and every spawn script child. It is also thrown when script.spawn does not allow the $entry path, or the file does not compile. A second call in the same request also throws it.
  • LogicErrorAn $arguments value cannot be copied into an isolate. Examples are a resource, or a secret that is not revealed. For a static method entry, it is also thrown when $arguments is missing one of the method's parameters or has a name the method does not have.

Examples

Opening an order stream

<?nvs
// Shows how a request opens an event stream that runs another file.
// The path starts at the folder of this file.
// The stream starts when this request ends.
Core\Sse::upgrade('streams/orders.nvs');
// The `.nvse` file beside this example publishes two updates on `orders`.
// `orders.nvs` then prints one `order` event for each update.
Output
event: order
data: order 1042 was paid
event: order
data: order 1042 has shipped

Only one stream per request

<?nvs
// Shows that one request can open only one event stream.
class Clock
{
// The stream runs this method. It sends one event and then ends.
public static function run(): void
{
Core\Sse::current()->send('12:00');
}
}
Core\Sse::upgrade(Clock::run);
// A second call throws a `RuntimeError`. The first stream still starts.
try {
Core\Sse::upgrade(Clock::run);
} catch (RuntimeError $error) {
// This prints `second stream: not opened`.
echo "second stream: not opened\n";
}
// Then the stream prints `data: 12:00`.
Output
second stream: not opened
data: 12:00

Notifications for a signed in user

<?nvs
// Shows a stream of notifications that only the signed-in user receives.
class Notifications
{
// The stream runs this method. `arguments:` gives each parameter by its name.
public static function run(string $user): void
{
Core\Sse $sse = Core\Sse::current();
// Each user has their own topic, so a user sees only their own notifications.
Core\Topic::subscribe('user:' . $user);
?Core\Sse\Message $message = $sse->receive();
while ($message != null) {
$sse->send($message->value() as string, event: 'notification');
$message = $sse->receive();
}
}
}
// This array gives the user for each session identifier in the `session` cookie.
array<string> $users = ['c81f02' => 'ana', '5be9d4' => 'ben'];
tainted string $session = Core\Request::cookie('session') ?? '';
string $user = $users[$session] ?? '';
// Only a signed-in user gets a stream. Everybody else gets an error page.
if ($user == '') {
echo "401: sign in first\n";
} else {
Core\Sse::upgrade(Notifications::run, arguments: ['user' => $user]);
}
// The `.nvse` file publishes two notifications for Ana and one for Ben.
// This prints only Ana's two, as `notification` events.
Output
event: notification
data: Your order has shipped
event: notification
data: You have a new message