Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Enhance eventStream to Support Custom Events and Start Messages #54776

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Illuminate/Http/StreamedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Illuminate\Http;

class StreamedEvent
{
/**
* The name of the event.
*/
public string $event;

/**
* The data of the stream.
*/
public mixed $data;

/**
* Create a new streamed event instance.
*
* @return void
*/
public function __construct(string $event, mixed $data)
{
$this->event = $event;
$this->data = $data;
}
}
33 changes: 25 additions & 8 deletions src/Illuminate/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Http\StreamedEvent;
use Illuminate\Routing\Exceptions\StreamedResponseException;
use Illuminate\Support\Js;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -124,35 +125,51 @@ public function jsonp($callback, $data = [], $status = 200, array $headers = [],
*
* @param \Closure $callback
* @param array $headers
* @param string $endStreamWith
* @param \Illuminate\Http\StreamedEvent|string $endStreamWith
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function eventStream(Closure $callback, array $headers = [], string $endStreamWith = '</stream>')
public function eventStream(Closure $callback, array $headers = [], StreamedEvent|string|null $endStreamWith = '</stream>')
{
return $this->stream(function () use ($callback, $endStreamWith) {
foreach ($callback() as $message) {
if (connection_aborted()) {
break;
}

$event = 'update';

if ($message instanceof StreamedEvent) {
$event = $message->event;
$message = $message->data;
}

if (! is_string($message) && ! is_numeric($message)) {
$message = Js::encode($message);
}

echo "event: update\n";
echo "event: $event\n";
echo 'data: '.$message;
echo "\n\n";

ob_flush();
flush();
}

echo "event: update\n";
echo 'data: '.$endStreamWith;
echo "\n\n";
if (filled($endStreamWith)) {
$endEvent = 'update';

if ($endStreamWith instanceof StreamedEvent) {
$endEvent = $endStreamWith->event;
$endStreamWith = $endStreamWith->data;
}

echo "event: $endEvent\n";
echo 'data: '.$endStreamWith;
echo "\n\n";

ob_flush();
flush();
ob_flush();
flush();
}
}, 200, array_merge($headers, [
'Content-Type' => 'text/event-stream',
'Cache-Control' => 'no-cache',
Expand Down
Loading