Skip to content

Simple EventSource client implementation to consume streaming Server-Sent Events (SSE) with ReactPHP.

License

Notifications You must be signed in to change notification settings

bpolaszek/reactphp-eventsource

 
 

Repository files navigation

clue/reactphp-eventsource Build Status

Event-driven EventSource client, receiving streaming messages from any HTML5 Server-Sent Events (SSE) server, built on top of ReactPHP.

Note: This project is in early alpha stage! Feel free to report any issues you encounter.

Table of contents

Quickstart example

Once installed, you can use the following code to stream messages from any Server-Sent Events (SSE) server endpoint:

$loop = Factory::create();
$es = new Clue\React\EventSource\EventSource('https://example.com/stream.php', $loop);

$es->on('message', function (Clue\React\EventSource\MessageEvent $message) {
    //$data = json_decode($message->data);
    var_dump($message);
});

$loop->run();

See the examples.

Usage

EventSource

The EventSource class is responsible for communication with the remote Server-Sent Events (SSE) endpoint.

The EventSource object works very similar to the one found in common web browsers. Unless otherwise noted, it follows the same semantics as defined under https://html.spec.whatwg.org/multipage/server-sent-events.html

It requires the URL to the remote Server-Sent Events (SSE) endpoint and also registers everything with the main EventLoop in order to handle async HTTP requests.

$loop = React\EventLoop\Factory::create();

$es = new Clue\React\EventSource\EventSource('https://example.com/stream.php', $loop);

If you need custom connector settings (DNS resolution, TLS parameters, timeouts, proxy servers etc.), you can explicitly pass a custom instance of the ConnectorInterface to the Browser instance and pass it as an additional argument to the EventSource like this:

$connector = new React\Socket\Connector($loop, array(
    'dns' => '127.0.0.1',
    'tcp' => array(
        'bindto' => '192.168.10.1:0'
    ),
    'tls' => array(
        'verify_peer' => false,
        'verify_peer_name' => false
    )
));
$browser = new React\Http\Browser($loop, $connector);

$es = new Clue\React\EventSource\EventSource('https://example.com/stream.php', $loop, $browser);

Install

The recommended way to install this library is through Composer. New to Composer?

This will install the latest supported version:

$ composer require clue/reactphp-eventsource:dev-master

This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.4 through current PHP 7+. It's highly recommended to use PHP 7+ for this project.

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

$ composer install

To run the test suite, go to the project root and run:

$ php vendor/bin/phpunit

License

This project is released under the permissive MIT license.

Did you know that I offer custom development services and issuing invoices for sponsorships of releases and for contributions? Contact me (@clue) for details.

About

Simple EventSource client implementation to consume streaming Server-Sent Events (SSE) with ReactPHP.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%