{"id":13700126,"url":"https://github.com/hhxsv5/php-sse","last_synced_at":"2025-05-16T06:07:43.474Z","repository":{"id":48142883,"uuid":"32618745","full_name":"hhxsv5/php-sse","owner":"hhxsv5","description":"A simple and efficient library implemented HTML5's server-sent events by PHP, is used to real-time push events from server to client, and easier than Websocket, instead of AJAX request.","archived":false,"fork":false,"pushed_at":"2021-03-04T09:51:13.000Z","size":385,"stargazers_count":442,"open_issues_count":7,"forks_count":51,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-05-09T14:03:03.897Z","etag":null,"topics":["event-stream","events","eventsource","server-sent-events","sever-events","sse"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hhxsv5.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-21T03:38:28.000Z","updated_at":"2025-04-30T18:45:40.000Z","dependencies_parsed_at":"2022-09-16T12:41:01.171Z","dependency_job_id":null,"html_url":"https://github.com/hhxsv5/php-sse","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hhxsv5%2Fphp-sse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hhxsv5%2Fphp-sse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hhxsv5%2Fphp-sse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hhxsv5%2Fphp-sse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hhxsv5","download_url":"https://codeload.github.com/hhxsv5/php-sse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254478193,"owners_count":22077676,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["event-stream","events","eventsource","server-sent-events","sever-events","sse"],"created_at":"2024-08-02T20:00:48.858Z","updated_at":"2025-05-16T06:07:38.464Z","avatar_url":"https://github.com/hhxsv5.png","language":"PHP","funding_links":[],"categories":["Miscellaneous"],"sub_categories":[],"readme":"PHP SSE: Server-sent Events\n======\n\nA simple and efficient library implemented HTML5's server-sent events by PHP, is used to real-time push events from server to client, and easier than\nWebsocket, instead of AJAX request.\n\n## Requirements\n\n* PHP 5.4 or later\n\n## Installation via Composer([packagist](https://packagist.org/packages/hhxsv5/php-sse))\n\n```BASH\ncomposer require \"hhxsv5/php-sse:~2.0\" -vvv\n```\n\n## Usage\n\n### Run demo\n\n- Run PHP webserver\n\n```Bash\ncd examples\nphp -S 127.0.0.1:9001 -t .\n```\n\n- Open url `http://127.0.0.1:9001/index.html`\n\n![Demo](https://raw.githubusercontent.com/hhxsv5/php-sse/master/sse.png)\n\n### Javascript demo\n\n\u003e Client: receiving events from the server.\n\n```Javascript\n// withCredentials=true: pass the cross-domain cookies to server-side\nconst source = new EventSource('http://127.0.0.1:9001/sse.php', {withCredentials: true});\nsource.addEventListener('news', function (event) {\n    console.log(event.data);\n    // source.close(); // disconnect stream\n}, false);\n```\n\n### PHP demo\n\n\u003e Server: Sending events by pure php.\n\n```PHP\nuse Hhxsv5\\SSE\\Event;\nuse Hhxsv5\\SSE\\SSE;\nuse Hhxsv5\\SSE\\StopSSEException;\n\n// PHP-FPM SSE Example: push messages to client\n\nheader('Content-Type: text/event-stream');\nheader('Cache-Control: no-cache');\nheader('Connection: keep-alive');\nheader('X-Accel-Buffering: no'); // Nginx: unbuffered responses suitable for Comet and HTTP streaming applications\n\n$callback = function () {\n    $id = mt_rand(1, 1000);\n    $news = [['id' =\u003e $id, 'title' =\u003e 'title ' . $id, 'content' =\u003e 'content ' . $id]]; // Get news from database or service.\n    if (empty($news)) {\n        return false; // Return false if no new messages\n    }\n    $shouldStop = false; // Stop if something happens or to clear connection, browser will retry\n    if ($shouldStop) {\n        throw new StopSSEException();\n    }\n    return json_encode(compact('news'));\n    // return ['event' =\u003e 'ping', 'data' =\u003e 'ping data']; // Custom event temporarily: send ping event\n    // return ['id' =\u003e uniqid(), 'data' =\u003e json_encode(compact('news'))]; // Custom event Id\n};\n(new SSE(new Event($callback, 'news')))-\u003estart();\n```\n\n### Symfony and Laravel demo\n\n\u003e Server: Sending events by Laravel or Symfony.\n\n```PHP\nuse Hhxsv5\\SSE\\SSE;\nuse Hhxsv5\\SSE\\Event;\nuse Hhxsv5\\SSE\\StopSSEException;\n\n// Action method in controller\npublic function getNewsStream()\n{\n    $response = new \\Symfony\\Component\\HttpFoundation\\StreamedResponse();\n    $response-\u003eheaders-\u003eset('Content-Type', 'text/event-stream');\n    $response-\u003eheaders-\u003eset('Cache-Control', 'no-cache');\n    $response-\u003eheaders-\u003eset('Connection', 'keep-alive');\n    $response-\u003eheaders-\u003eset('X-Accel-Buffering', 'no'); // Nginx: unbuffered responses suitable for Comet and HTTP streaming applications\n    $response-\u003esetCallback(function () {\n        $callback = function () {\n            $id = mt_rand(1, 1000);\n            $news = [['id' =\u003e $id, 'title' =\u003e 'title ' . $id, 'content' =\u003e 'content ' . $id]]; // Get news from database or service.\n            if (empty($news)) {\n                return false; // Return false if no new messages\n            }\n            $shouldStop = false; // Stop if something happens or to clear connection, browser will retry\n            if ($shouldStop) {\n                throw new StopSSEException();\n            }\n            return json_encode(compact('news'));\n            // return ['event' =\u003e 'ping', 'data' =\u003e 'ping data']; // Custom event temporarily: send ping event\n            // return ['id' =\u003e uniqid(), 'data' =\u003e json_encode(compact('news'))]; // Custom event Id\n        };\n        (new SSE(new Event($callback, 'news')))-\u003estart();\n    });\n    return $response;\n}\n```\n\n### Swoole demo\n\n\u003e Server: Sending events by Swoole Coroutine Http Server.\n\u003e Install [Swoole](https://github.com/swoole/swoole-src) 4.5.x: `pecl install swoole`.\n\n```php\nuse Hhxsv5\\SSE\\Event;\nuse Hhxsv5\\SSE\\SSESwoole;\nuse Swoole\\Http\\Request;\nuse Swoole\\Http\\Response;\nuse Swoole\\Http\\Server;\nuse Hhxsv5\\SSE\\StopSSEException;\n\n// Swoole SSE Example: push messages to client\n\n$server = new Server('0.0.0.0', 5200);\n$server-\u003eset([\n    'enable_coroutine'   =\u003e true,\n    'max_coroutine'      =\u003e 10000, // worker_num*10000\n    'reactor_num'        =\u003e swoole_cpu_num() * 2,\n    'worker_num'         =\u003e swoole_cpu_num() * 2,\n    'max_request'        =\u003e 100000,\n    'buffer_output_size' =\u003e 4 * 1024 * 1024, // 4MB\n    'log_level'          =\u003e SWOOLE_LOG_WARNING,\n    'log_file'           =\u003e __DIR__ . '/swoole.log',\n]);\n\n$server-\u003eon('Request', function (Request $request, Response $response) use ($server) {\n    $response-\u003eheader('Access-Control-Allow-Origin', '*');\n    $response-\u003eheader('Content-Type', 'text/event-stream');\n    $response-\u003eheader('Cache-Control', 'no-cache');\n    $response-\u003eheader('Connection', 'keep-alive');\n    $response-\u003eheader('X-Accel-Buffering', 'no');\n\n    $event = new Event(function () {\n        $id = mt_rand(1, 1000);\n        $news = [['id' =\u003e $id, 'title' =\u003e 'title ' . $id, 'content' =\u003e 'content ' . $id]]; // Get news from database or service.\n        if (empty($news)) {\n            return false; // Return false if no new messages\n        }\n        $shouldStop = false; // Stop if something happens or to clear connection, browser will retry\n        if ($shouldStop) {\n            throw new StopSSEException();\n        }\n        return json_encode(compact('news'));\n        // return ['event' =\u003e 'ping', 'data' =\u003e 'ping data']; // Custom event temporarily: send ping event\n        // return ['id' =\u003e uniqid(), 'data' =\u003e json_encode(compact('news'))]; // Custom event Id\n    }, 'news');\n    (new SSESwoole($event, $request, $response))-\u003estart();\n});\n$server-\u003estart();\n```\n\n## License\n\n[MIT](https://github.com/hhxsv5/php-sse/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhhxsv5%2Fphp-sse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhhxsv5%2Fphp-sse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhhxsv5%2Fphp-sse/lists"}