@@ -27,7 +27,7 @@ php -S 127.0.0.1:9001 -t .
2727![ Demo] ( https://raw.githubusercontent.com/hhxsv5/SSE/master/sse.png )
2828
2929### Javascript demo
30- > Client: receiving events from the server
30+ > Client: receiving events from the server.
3131
3232``` Javascript
3333// withCredentials=true: pass the cross-domain cookies to server-side
@@ -38,59 +38,55 @@ source.addEventListener("new-msgs", function(event){
3838```
3939
4040### PHP demo
41- > Server: sending events from the server by pure php
41+ > Server: sending events from the server by pure php.
4242
4343``` PHP
44- include './vendor/autoload.php';
44+ include '.. /vendor/autoload.php';
4545
4646use Hhxsv5\SSE\SSE;
4747use Hhxsv5\SSE\Update;
4848
49+ // Example: push messages to client
50+
4951header('Content-Type: text/event-stream');
5052header('Cache-Control: no-cache');
5153header('Connection: keep-alive');
52- header('X-Accel-Buffering: no');// Nginx: unbuffered responses suitable for Comet and HTTP streaming applications
54+ header('X-Accel-Buffering: no'); // Nginx: unbuffered responses suitable for Comet and HTTP streaming applications
5355
5456(new SSE())->start(new Update(function () {
5557 $id = mt_rand(1, 1000);
56- $newMsgs = [
57- [
58- 'id' => $id,
59- 'title' => 'title' . $id,
60- 'content' => 'content' . $id,
61- ],
62- ];//get data from database or service.
63- if (!empty($newMsgs)) {
64- return json_encode(['newMsgs' => $newMsgs]);
58+ $news = [['id' => $id, 'title' => 'title ' . $id, 'content' => 'content ' . $id]]; // Get news from database or service.
59+ if (!empty($news)) {
60+ return json_encode(compact('news'));
6561 }
66- return false;//return false if no new messages
67- }), 'new-msgs ');
62+ return false; // Return false if no new messages
63+ }), 'news ');
6864```
6965
7066### Symfony and Laravel demo
71- > Server: sending events from the server by Laravel or Symfony
67+ > Server: sending events from the server by Laravel or Symfony.
7268
7369``` PHP
7470use Hhxsv5\SSE\SSE;
7571use Hhxsv5\SSE\Update;
7672
77- //Action method in the controller
78- public function newMsgs ()
73+ // Action method in controller
74+ public function getNewsStream ()
7975{
8076 $response = new \Symfony\Component\HttpFoundation\StreamedResponse();
8177 $response->headers->set('Content-Type', 'text/event-stream');
8278 $response->headers->set('Cache-Control', 'no-cache');
8379 $response->headers->set('Connection', 'keep-alive');
84- $response->headers->set('X-Accel-Buffering', 'no');// Nginx: unbuffered responses suitable for Comet and HTTP streaming applications
80+ $response->headers->set('X-Accel-Buffering', 'no'); // Nginx: unbuffered responses suitable for Comet and HTTP streaming applications
8581 $response->setCallback(function () {
8682 (new SSE())->start(new Update(function () {
8783 $id = mt_rand(1, 1000);
88- $newMsgs = [['id' => $id, 'title' => 'title' . $id, 'content' => 'content' . $id]];//get data from database or service.
89- if (!empty($newMsgs )) {
90- return json_encode(['newMsgs' => $newMsgs] );
84+ $news = [['id' => $id, 'title' => 'title ' . $id, 'content' => 'content ' . $id]]; // Get news from database or service.
85+ if (!empty($news )) {
86+ return json_encode(compact('news') );
9187 }
92- return false;//return false if no new messages
93- }), 'new-msgs ');
88+ return false; // Return false if no new messages
89+ }), 'news ');
9490 });
9591 return $response;
9692}
0 commit comments