You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
Do you know how we can play with SSE ?
I'm trying to use CallbackStream to handle my SSE manager with code like this one:
$stream = new CallbackStream( function () use ( $that ) {
$that->setStart( time() );
echo 'retry: ' . ( $that->client_reconnect * 1000 ) . "\n"; // Set the retry interval for the client
while ( true ) {
// Leave the loop if there are no more handlers
if ( !$that->hasEventListener() ) {
break;
}
if ( $that->isTick() ) {
// No updates needed, send a comment to keep the connection alive.
// From https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events
echo ': ' . sha1( mt_rand() ) . "\n\n";
}
// Start to check for updates
foreach ( $that->getEventListeners() as $event => $handler ) {
if ( $handler->check() ) { // Check if the data is avaliable
$data = $handler->update(); // Get the data
$id = $that->getNewId();
$that->sendBlock( $id, $data, $event );
// Make sure the data has been sent to the client
$that->flush();
}
}
// Break if the time exceed the limit
if ( $that->exec_limit !== 0 && $that->getUptime() > $that->exec_limit ) {
break;
}
// Sleep
$that->sleep();
}
} );
$response = new Response();
$response->withHeader( 'Content-Type', 'text/event-stream' )
->withHeader( 'Cache-Control', 'no-cache' )
->withHeader( 'X-Accel-Buffering', 'no' );
if ( $this->allow_cors ) {
$response->withHeader( 'Access-Control-Allow-Origin', '*' );
$response->withHeader( 'Access-Control-Allow-Credentials', 'true' );
}
if( $this->use_chunked_encoding ) {
$response->withHeader( 'Transfer-encoding', 'chunked' );
}
return $response->withStatus( 200 )->withBody( $stream );
How can I use echo and flush behavior with CallbackStream ?
Regards,
The text was updated successfully, but these errors were encountered:
Ocramius
changed the title
CallbakcStream and Server Sent Event (SSE) ?
CallbackStream and Server Sent Event (SSE) ?
Jan 29, 2017
@Wikiki I would probably use a custom readable Stream for the body, rather than a callback stream. That should be very much sufficient, since SSE doesn't need client interaction.
In order to do that, you'd probably implement a custom eof and a custom read
Hi,
Do you know how we can play with SSE ?
I'm trying to use CallbackStream to handle my SSE manager with code like this one:
How can I use echo and flush behavior with CallbackStream ?
Regards,
The text was updated successfully, but these errors were encountered: