Skip to content

Commit a90e202

Browse files
committed
document event stream customization
1 parent 78ba6ed commit a90e202

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

responses.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,18 @@ Route::get('/chat', function () {
417417
});
418418
```
419419

420-
This event stream may be consumed via an [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) object by your application's frontend. The `eventStream` method will automatically send a `</stream>` update to the event stream when the stream is complete:
420+
If you would like to customize the name of the event, you may yield an instance of the `StreamedEvent` class:
421+
422+
```php
423+
use Illuminate\Http\StreamedEvent;
424+
425+
yield new StreamedEvent(
426+
event: 'update',
427+
data: $response->choices[0],
428+
);
429+
```
430+
431+
Event streams may be consumed via an [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) object by your application's frontend. The `eventStream` method will automatically send a `</stream>` update to the event stream when the stream is complete:
421432

422433
```js
423434
const source = new EventSource('/chat');
@@ -433,6 +444,14 @@ source.addEventListener('update', (event) => {
433444
})
434445
```
435446

447+
To customize the final event that is sent to the event stream, you may provide a `StreamedEvent` instance to the `eventStream` method's `endStreamWith` argument:
448+
449+
```php
450+
return response()->eventStream(function () {
451+
// ...
452+
}, endStreamWith: new StreamedEvent(event: 'update', data: '</stream>'));
453+
```
454+
436455
<a name="streamed-downloads"></a>
437456
#### Streamed Downloads
438457

0 commit comments

Comments
 (0)