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

How to handle SSE response ? #228

Closed
Wikiki opened this issue Jan 28, 2017 · 1 comment
Closed

How to handle SSE response ? #228

Wikiki opened this issue Jan 28, 2017 · 1 comment
Assignees

Comments

@Wikiki
Copy link

Wikiki commented Jan 28, 2017

Hi,

I'm trying to play with SSE (Server Side Events) and I need help to figure out how I can send an SSE response with Diactoros ?

I tried to create a new response type as following:

class SseResponse extends Response {

  use InjectContentTypeTrait;

  /**
   * Create an HTML response.
   *
   * Produces an HTML response with a Content-Type of text/html and a default
   * status of 200.
   *
   * @param string|StreamInterface $html HTML or stream for the message body.
   * @param int $status Integer status code for the response; 200 by default.
   * @param array $headers Array of headers to use at initialization.
   * @throws InvalidArgumentException if $html is neither a string or stream.
   */
  public function __construct( $callback, array $headers = [] ) {
    $this->callback = $callback;
    parent::__construct(
      $this->createBody(),
      200,
      $this->injectContentType( 'text/event-stream', $headers )
    );
  }

  /**
   * Create the message body.
   *
   * @param string|StreamInterface $html
   * @return StreamInterface
   * @throws InvalidArgumentException if $html is neither a string or stream.
   */
  private function createBody() {
    $body = new Stream( 'php://temp', 'wb+' );
    $body->rewind();
    return $body;
  }

  public function getBody() {
    call_user_func( $this->callback );
  }
}

But without success. Do you have any idea how I can handle this kind of response ?

Regards,

@Wikiki Wikiki closed this as completed Jan 28, 2017
@Ocramius
Copy link
Member

Moved to #229

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

No branches or pull requests

2 participants