Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

CallbackStream and Server Sent Event (SSE) ? #229

Open
Wikiki opened this issue Jan 28, 2017 · 2 comments
Open

CallbackStream and Server Sent Event (SSE) ? #229

Wikiki opened this issue Jan 28, 2017 · 2 comments

Comments

@Wikiki
Copy link

Wikiki commented Jan 28, 2017

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:

$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,

@Ocramius Ocramius changed the title CallbakcStream and Server Sent Event (SSE) ? CallbackStream and Server Sent Event (SSE) ? Jan 29, 2017
@Ocramius
Copy link
Member

@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

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-diactoros; a new issue has been opened at laminas/laminas-diactoros#16.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants