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. Astringis sent as it is. Any other value is encoded as JSON, the same way as byCore\Json::encode. Ataintedvalue is allowed. The data is cleaned and split intodata:lines, so it cannot change any other field.$event?stringdefault nullThe event name the client listens for, ornullfor the defaultmessage. An attacker could use this name to control the client. So ataintedvalue does not compile here.$id?stringdefault nullThe id the client sends back inLast-Event-IDwhen it reconnects, ornullfor no id. Ataintedvalue 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$eventor$idcontains 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");data: Your order has shipped.
data: Line onedata: Line twoA 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);event: chatdata: {"user":"anna","text":"Hello!"}
data: 42New 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);}event: orderid: 1041data: {"number":"1041","total":1999}
event: orderid: 1042data: {"number":"1042","total":4550}