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

CallbackStream and Server Sent Event (SSE) ? #16

Closed
weierophinney opened this issue Dec 31, 2019 · 1 comment
Closed

CallbackStream and Server Sent Event (SSE) ? #16

weierophinney opened this issue Dec 31, 2019 · 1 comment
Labels
Question Further information is requested

Comments

@weierophinney
Copy link
Member

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,


Originally posted by @Wikiki at zendframework/zend-diactoros#229

@weierophinney
Copy link
Member Author

@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


Originally posted by @Ocramius at zendframework/zend-diactoros#229 (comment)

@Xerkus Xerkus added the Question Further information is requested label May 1, 2023
@Xerkus Xerkus closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants