Skip to content

Core\Sse::send

$sse->send(mixed $data, ?string $event = null, ?string $id = null): void$sse->send(
    mixed $data,
    ?string $event = null,
    ?string $id = null,
): void

Sends one event to the client on an event stream.

The first argument is the data of the event. A string is sent as it is. Any other value, such as an array or a number, is converted to JSON first. Each line of the data becomes its own data: line, and an empty line ends the event.

Two optional arguments go with the data. event is a name: the browser runs the listener added for that name, and without a name it runs onmessage. id is sent back by the browser in the Last-Event-ID header when it reconnects, so the server can continue from there.

A name or an id with a line break or a NUL byte throws a LogicError, and so does empty data. Nothing is sent in that case.

The examples below send a text, send an array as JSON under a name, and send a shop's new orders with ids.

Parameters

  • $datamixedThe data the client receives. A string is sent as it is. Any other value is encoded as JSON, the same way as by Core\Json::encode. A tainted value is allowed. The data is cleaned and split into data: lines, so it cannot change any other field.
  • $event?stringdefault nullThe event name the client listens for, or null for the default message. An attacker could use this name to control the client. So a tainted value does not compile here.
  • $id?stringdefault nullThe id the client sends back in Last-Event-ID when it reconnects, or null for no id. A tainted value does not compile here, for the same reason. The server keeps no old events. To resume, your handler reads the header and uses your own event log.

Return value

Returns void. Nothing. The event is formatted and added to the response body.

Errors

  • LogicError$event or $id contains a line break or a NUL byte. Either one would end the event early and change what the client does. It is also thrown for an empty $data, because a client ignores an event with no data.

Examples

Sending a text

<?nvs
// Shows how to send a text to the client as one event.
Core\Sse $sse = Core\Sse::startStream();
// This prints `data: Your order has shipped.` and an empty line.
// The empty line ends the event.
$sse->send('Your order has shipped.');
// A text with a line break is still one event. Each line gets its own `data:` line.
$sse->send("Line one\nLine two");
Output
data: Your order has shipped.
data: Line one
data: Line two

A named event with json

<?nvs
// Shows how to send an array as JSON, with an event name the browser listens for.
Core\Sse $sse = Core\Sse::startStream();
// An array is not a string, so `send()` converts it to JSON.
// The `event:` line gives the name. In the browser, `addEventListener("chat", ...)` receives it.
$sse->send(['user' => 'anna', 'text' => 'Hello!'], event: 'chat');
// Without a name, the browser's `onmessage` handler receives the event.
$sse->send(42);
Output
event: chat
data: {"user":"anna","text":"Hello!"}
data: 42

New orders with ids

<?nvs
// Shows a feed of new orders for a shop's dashboard. Each event has an id.
// The order number, and the total in cents.
array<int> $totals = ['1041' => 1999, '1042' => 4550];
Core\Sse $sse = Core\Sse::startStream();
// Each event gets the order number as its `id:`. If the connection breaks, the browser
// sends the last id back in the `Last-Event-ID` header, so the server knows where to continue.
foreach ($totals as string $number => int $total) {
$sse->send(['number' => $number, 'total' => $total], event: 'order', id: $number);
}
Output
event: order
id: 1041
data: {"number":"1041","total":1999}
event: order
id: 1042
data: {"number":"1042","total":4550}