@@ -13,6 +13,16 @@ built on top of [ReactPHP](https://reactphp.org/).
1313* [ Quickstart example] ( #quickstart-example )
1414* [ Usage] ( #usage )
1515 * [ EventSource] ( #eventsource )
16+ * [ message event] ( #message-event )
17+ * [ open event] ( #open-event )
18+ * [ error event] ( #error-event )
19+ * [ EventSource::$readyState] ( #eventsourcereadystate )
20+ * [ EventSource::$url] ( #eventsourceurl )
21+ * [ close()] ( #close )
22+ * [ MessageEvent] ( #messageevent )
23+ * [ MessageEvent::$data] ( #messageeventdata )
24+ * [ MessageEvent::$lastEventId] ( #messageeventlasteventid )
25+ * [ MessageEvent::$type] ( #messageeventtype )
1626* [ Install] ( #install )
1727* [ Tests] ( #tests )
1828* [ License] ( #license )
@@ -22,16 +32,28 @@ built on top of [ReactPHP](https://reactphp.org/).
2232Once [ installed] ( #install ) , you can use the following code to stream messages
2333from any Server-Sent Events (SSE) server endpoint:
2434
35+ ```
36+ data: {"name":"Alice","message":"Hello everybody!"}
37+
38+ data: {"name":"Bob","message":"Hey Alice!"}
39+
40+ data: {"name":"Carol","message":"Nice to see you Alice!"}
41+
42+ data: {"name":"Alice","message":"What a lovely chat!"}
43+
44+ data: {"name":"Bob","message":"Yeah, all powered by ReactPHP, such an awesome piece of technology :)"}
45+ ```
46+
2547``` php
2648$es = new Clue\React\EventSource\EventSource('https://example.com/stream.php');
2749
2850$es->on('message', function (Clue\React\EventSource\MessageEvent $message) {
29- //$data = json_decode($message->data);
30- var_dump($ message) ;
51+ $json = json_decode($message->data);
52+ echo $json->name . ': ' . $json-> message . PHP_EOL ;
3153});
3254```
3355
34- See the [ examples] ( examples ) .
56+ See the [ examples] ( examples/ ) .
3557
3658## Usage
3759
@@ -77,6 +99,205 @@ here in order to use the [default loop](https://github.com/reactphp/event-loop#l
7799This value SHOULD NOT be given unless you're sure you want to explicitly use a
78100given event loop instance.
79101
102+ #### message event
103+
104+ The ` message ` event will be emitted whenever an EventSource message is received.
105+
106+ ``` php
107+ $es->on('message', function (Clue\React\EventSource\MessageEvent $message) {
108+ // $json = json_decode($message->data);
109+ var_dump($message);
110+ });
111+ ```
112+
113+ The EventSource stream may emit any number of messages over its lifetime. Each
114+ ` message ` event will receive a [ ` MessageEvent ` object] ( #messageevent ) .
115+
116+ The [ ` MessageEvent::$data ` property] ( #messageeventdata ) can be used to access
117+ the message payload data. It is commonly used for transporting structured data
118+ such as JSON:
119+
120+ ```
121+ data: {"name":"Alice","age":30}
122+
123+ data: {"name":"Bob","age":50}
124+ ```
125+ ``` php
126+ $es->on('message', function (Clue\React\EventSource\MessageEvent $message) {
127+ $json = json_decode($message->data);
128+ echo "{$json->name} is {$json->age} years old" . PHP_EOL;
129+ });
130+ ```
131+
132+ The EventSource stream may specify an event type for each incoming message. This
133+ ` event ` field can be used to emit appropriate event types like this:
134+
135+ ```
136+ data: Alice
137+ event: join
138+
139+ data: Hello!
140+ event: chat
141+
142+ data: Bob
143+ event: leave
144+ ```
145+ ``` php
146+ $es->on('join', function (Clue\React\EventSource\MessageEvent $message) {
147+ echo $message->data . ' joined' . PHP_EOL;
148+ });
149+
150+ $es->on('chat', function (Clue\React\EventSource\MessageEvent $message) {
151+ echo 'Message: ' . $message->data . PHP_EOL;
152+ });
153+
154+ $es->on('leave', function (Clue\React\EventSource\MessageEvent $message) {
155+ echo $message->data . ' left' . PHP_EOL;
156+ });
157+ ```
158+
159+ See also [ ` MessageEvent::$type ` property] ( #messageeventtype ) for more details.
160+
161+ #### open event
162+
163+ The ` open ` event will be emitted when the EventSource connection is successfully established.
164+
165+ ``` php
166+ $es->on('open', function () {
167+ echo 'Connection opened' . PHP_EOL;
168+ });
169+ ```
170+
171+ Once the EventSource connection is open, it may emit any number of
172+ [ ` message ` events] ( #message-event ) .
173+
174+ If the connection can not be opened successfully, it will emit an
175+ [ ` error ` event] ( #error-event ) instead.
176+
177+ #### error event
178+
179+ The ` error ` event will be emitted when the EventSource connection fails.
180+ The event receives a single ` Exception ` argument for the error instance.
181+
182+ ``` php
183+ $redis->on('error', function (Exception $e) {
184+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
185+ });
186+ ```
187+
188+ The EventSource connection will be retried automatically when it is temporarily
189+ disconnected. If the server sends a non-successful HTTP status code or an
190+ invalid ` Content-Type ` response header, the connection will fail permanently.
191+
192+ ``` php
193+ $es->on('error', function (Exception $e) use ($es) {
194+ if ($es->readyState === Clue\React\EventSource\EventSource::CLOSED) {
195+ echo 'Permanent error: ' . $e->getMessage() . PHP_EOL;
196+ } else {
197+ echo 'Temporary error: ' . $e->getMessage() . PHP_EOL;
198+ }
199+ });
200+ ```
201+
202+ See also the [ ` EventSource::$readyState ` property] ( #eventsourcereadystate ) .
203+
204+ #### EventSource::$readyState
205+
206+ The ` int $readyState ` property can be used to
207+ check the current EventSource connection state.
208+
209+ The state is read-only and can be in one of three states over its lifetime:
210+
211+ * ` EventSource::CONNECTING `
212+ * ` EventSource::OPEN `
213+ * ` EventSource::CLOSED `
214+
215+ #### EventSource::$url
216+
217+ The ` readonly string $url ` property can be used to
218+ get the EventSource URL as given to the constructor.
219+
220+ #### close()
221+
222+ The ` close(): void ` method can be used to
223+ forcefully close the EventSource connection.
224+
225+ This will close any active connections or connection attempts and go into the
226+ ` EventSource::CLOSED ` state.
227+
228+ ### MessageEvent
229+
230+ The ` MessageEvent ` class represents an incoming EventSource message.
231+
232+ #### MessageEvent::$data
233+
234+ The ` readonly string $data ` property can be used to
235+ access the message payload data.
236+
237+ ```
238+ data: hello
239+ ```
240+ ``` php
241+ assert($message->data === 'hello');
242+ ```
243+
244+ The ` data ` field may also span multiple lines. This is commonly used for
245+ transporting structured data such as JSON:
246+
247+ ```
248+ data: {
249+ data: "message": "hello"
250+ data: }
251+ ```
252+ ``` php
253+ $json = json_decode($message->data);
254+ assert($json->message === 'hello');
255+ ```
256+
257+ If the message does not contain a ` data ` field or the ` data ` field is empty, the
258+ message will be discarded without emitting an event.
259+
260+ #### MessageEvent::$lastEventId
261+
262+ The ` readonly string $lastEventId ` property can be used to
263+ access the last event ID.
264+
265+ ```
266+ data: hello
267+ id: 1
268+ ```
269+ ``` php
270+ assert($message->data === 'hello');
271+ assert($message->lastEventId === '1');
272+ ```
273+
274+ Internally, the ` id ` field will automatically be used as the ` Last-Event-ID ` HTTP
275+ request header in case the connection is interrupted.
276+
277+ If the message does not contain an ` id ` field, the ` $lastEventId ` property will
278+ be the value of the last ID received. If no previous message contained an ID, it
279+ will default to an empty string.
280+
281+ #### MessageEvent::$type
282+
283+ The ` readonly string $type ` property can be used to
284+ access the message event type.
285+
286+ ```
287+ data: Alice
288+ event: join
289+ ```
290+ ``` php
291+ assert($message->data === 'Alice');
292+ assert($message->type === 'join');
293+ ```
294+
295+ Internally, the ` event ` field will be used to emit the appropriate event type.
296+ See also [ ` message ` event] ( #message-event ) .
297+
298+ If the message does not contain a ` event ` field or the ` event ` field is empty,
299+ the ` $type ` property will default to ` message ` .
300+
80301## Install
81302
82303The recommended way to install this library is [ through Composer] ( https://getcomposer.org/ ) .
@@ -105,7 +326,7 @@ $ composer install
105326To run the test suite, go to the project root and run:
106327
107328``` bash
108- $ php vendor/bin/phpunit
329+ $ vendor/bin/phpunit
109330```
110331
111332## License
0 commit comments