Hosmel Quintana
Introduction
Installation
Basic usage
Connecting
Reading Events
Handling Errors
Advanced usage
Timeouts
Reconnecting
PSR-7 Responses
View on GitHub

Introduction

PHP SSE connects to Server-Sent Events endpoints with Guzzle and yields each event as the server sends it.

PHP SSE connects to Server-Sent Events (SSE) endpoints with Guzzle and yields each event as the server sends it. It parses streams the way the WHATWG HTML standard describes, including every line ending, comments, event IDs, and reconnection times, so it works with notification feeds and with APIs that stream their responses.

Quick start

After installing the package, connect to an endpoint and iterate its events:

php
use HosmelQ\SSE\Client;

$source = new Client()->get('https://example.com/events');

foreach ($source->events() as $event) {
    echo $event->event.': '.$event->data.PHP_EOL;
}

The loop ends when the server closes the stream.

Next steps

  • Connecting covers POST requests, request options, and your own Guzzle client.
  • Reading Events describes the fields of each event and how to decode JSON data.
  • Handling Errors explains which exceptions to catch.
  • Reconnecting resumes a stream after it drops.

Credits

PHP SSE is based on httpx-sse.

Installation