Skip to content

Commit

Permalink
add Swoole demo
Browse files Browse the repository at this point in the history
  • Loading branch information
hhxsv5 committed Jun 16, 2020
1 parent 1a2a681 commit c71ea2c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
.idea/
composer.lock
composer.lock
examples/*.log
46 changes: 42 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ source.addEventListener('news', function(event) {
```

### PHP demo
> Server: sending events from the server by pure php.
> Server: Sending events by pure php.
```PHP
include '../vendor/autoload.php';

use Hhxsv5\SSE\SSE;
use Hhxsv5\SSE\Update;

Expand All @@ -65,7 +63,7 @@ header('X-Accel-Buffering: no'); // Nginx: unbuffered responses suitable for Com
```

### Symfony and Laravel demo
> Server: sending events from the server by Laravel or Symfony.
> Server: Sending events by Laravel or Symfony.
```PHP
use Hhxsv5\SSE\SSE;
Expand Down Expand Up @@ -93,6 +91,46 @@ public function getNewsStream()
}
```

### Swoole demo
> Server: Sending events by Swoole Coroutine Http Server.
> Install [Swoole](https://github.com/swoole/swoole-src) 4.5.x: `pecl install swoole`.
```php
use Hhxsv5\SSE\SSESwoole;
use Hhxsv5\SSE\Update;

// Swoole SSE Example: push messages to client

$server = new Swoole\Http\Server('0.0.0.0', 5200);
$server->set([
'enable_coroutine' => true,
'max_coroutine' => 10000, // worker_num*10000
'reactor_num' => swoole_cpu_num() * 2,
'worker_num' => swoole_cpu_num() * 2,
'max_request' => 100000,
'buffer_output_size' => 4 * 1024 * 1024, // 4MB
'log_level' => SWOOLE_LOG_WARNING,
'log_file' => __DIR__ . '/swoole.log',
]);
$server->on('Request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($server) {
// $response->header('Access-Control-Allow-Origin', '*');
$response->header('Content-Type', 'text/event-stream');
$response->header('Cache-Control', 'no-cache');
$response->header('Connection', 'keep-alive');
$response->header('X-Accel-Buffering', 'no');

(new SSESwoole($request, $response))->start(new Update(function () {
$id = mt_rand(1, 1000);
$news = [['id' => $id, 'title' => 'title ' . $id, 'content' => 'content ' . $id]]; // Get news from database or service.
if (empty($news)) {
return false; // Return false if no new messages
}
return json_encode(compact('news'));
}), 'news');
});
$server->start();
```

## License

[MIT](https://github.com/hhxsv5/php-sse/blob/master/LICENSE)
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "~6.0"
"phpunit/phpunit": "~6.0",
"swoole/ide-helper": "@dev"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>SSE example</h1>
const newsList = document.getElementById('news-list');
if (typeof (EventSource) !== 'undefined') {
const source = new EventSource('/push.php');
// const source = new EventSource('http://127.0.0.1:5200/');
source.onopen = function (event) {
console.log('onopen', event);
};
Expand Down
2 changes: 1 addition & 1 deletion examples/push.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Hhxsv5\SSE\SSE;
use Hhxsv5\SSE\Update;

// Example: push messages to client
// PHP-FPM SSE Example: push messages to client

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
Expand Down
36 changes: 36 additions & 0 deletions examples/sse-swoole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
include '../vendor/autoload.php';

use Hhxsv5\SSE\SSESwoole;
use Hhxsv5\SSE\Update;

// Swoole SSE Example: push messages to client

$server = new Swoole\Http\Server('0.0.0.0', 5200);
$server->set([
'enable_coroutine' => true,
'max_coroutine' => 10000, // worker_num*10000
'reactor_num' => swoole_cpu_num() * 2,
'worker_num' => swoole_cpu_num() * 2,
'max_request' => 100000,
'buffer_output_size' => 4 * 1024 * 1024, // 4MB
'log_level' => SWOOLE_LOG_WARNING,
'log_file' => __DIR__ . '/swoole.log',
]);
$server->on('Request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($server) {
// $response->header('Access-Control-Allow-Origin', '*');
$response->header('Content-Type', 'text/event-stream');
$response->header('Cache-Control', 'no-cache');
$response->header('Connection', 'keep-alive');
$response->header('X-Accel-Buffering', 'no');

(new SSESwoole($request, $response))->start(new Update(function () {
$id = mt_rand(1, 1000);
$news = [['id' => $id, 'title' => 'title ' . $id, 'content' => 'content ' . $id]]; // Get news from database or service.
if (empty($news)) {
return false; // Return false if no new messages
}
return json_encode(compact('news'));
}), 'news');
});
$server->start();
44 changes: 44 additions & 0 deletions src/SSESwoole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Hhxsv5\SSE;

use Swoole\Coroutine;
use Swoole\Http\Request;
use Swoole\Http\Response;

class SSESwoole
{
protected $request;
protected $response;

public function __construct(Request $request, Response $response)
{
$this->request = $request;
$this->response = $response;
}

public function start(Update $update, $eventType = null, $milliRetry = 2000)
{
while (true) {
$changedData = $update->getUpdatedData();
if ($changedData !== false) {
$event = [
'id' => uniqid('', true),
'type' => $eventType,
'data' => (string)$changedData,
'retry' => $milliRetry,
];
} else {
$event = [
'comment' => 'no update',
];
}
$success = $this->response->write(new Event($event));
if (!$success) {
$this->response->end();
return;
}
Coroutine::sleep($update->getCheckInterval());
}
}
}

0 comments on commit c71ea2c

Please sign in to comment.