{"id":18463728,"url":"https://github.com/commandstring/reactphp-sse","last_synced_at":"2025-04-28T13:33:55.411Z","repository":{"id":65504716,"uuid":"593517719","full_name":"CommandString/reactphp-sse","owner":"CommandString","description":"Server Side Events with React/Http","archived":false,"fork":false,"pushed_at":"2023-01-26T08:22:18.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-16T16:57:59.556Z","etag":null,"topics":[],"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/CommandString.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-26T07:43:34.000Z","updated_at":"2023-05-15T18:24:38.000Z","dependencies_parsed_at":"2023-02-14T15:15:31.481Z","dependency_job_id":null,"html_url":"https://github.com/CommandString/reactphp-sse","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommandString%2Freactphp-sse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommandString%2Freactphp-sse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommandString%2Freactphp-sse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommandString%2Freactphp-sse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CommandString","download_url":"https://codeload.github.com/CommandString/reactphp-sse/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251320122,"owners_count":21570521,"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":[],"created_at":"2024-11-06T09:07:48.382Z","updated_at":"2025-04-28T13:33:55.365Z","avatar_url":"https://github.com/CommandString.png","language":"PHP","readme":"# commandstring/sse\n\nServer-sent events with ReactPHP\n\n# Channel\nThis is where all clients are stored, you can perform actions on all clients from here or specific clients.\n\n# Client\nThis represents an active connection\n\n# Example\n\n```php\n# index.php\n\u003c?php\n\nuse CommandString\\Sse\\Channel;\nuse CommandString\\Sse\\Client;\nuse Psr\\Http\\Message\\ResponseInterface;\nuse React\\EventLoop\\Loop;\nuse React\\Http\\Message\\ServerRequest;\nuse React\\Stream\\ThroughStream;\n\nrequire_once \"vendor/autoload.php\";\n\n$loop = Loop::get();\n\n$channel = new Channel;\n\n$clientCount = 0;\n(new React\\Http\\HttpServer(function (ServerRequest $req) use (\u0026$clientCount, $channel): ResponseInterface\n{\n    if ($req-\u003egetRequestTarget() === \"/sse\") {\n        $stream = new ThroughStream();\n        $client = new Client($stream, $clientCount++);\n\n        echo $clientCount . PHP_EOL;\n\n        $channel-\u003eaddClient($client);\n\n        return new React\\Http\\Message\\Response(\n             React\\Http\\Message\\Response::STATUS_OK,\n            array(\n                'Content-Type' =\u003e 'text/event-stream'\n            ),\n            $stream\n        );\n    }\n\n    return new React\\Http\\Message\\Response(\n        React\\Http\\Message\\Response::STATUS_OK,\n        array(\n            'Content-Type' =\u003e 'text/html'\n        ),\n        file_get_contents(\"index.html\")\n    );\n}))-\u003elisten(new React\\Socket\\SocketServer('127.0.0.1:8000'));\n\nLoop::addPeriodicTimer(1, function () use ($channel) {\n    $channel-\u003esendMessageToAll(mt_rand(0, 9999), \"item\");\n});\n```\n\n```html\n\u003c!-- index.html --\u003e\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\n\u003chead\u003e\n    \u003cmeta charset=\"UTF-8\"\u003e\n    \u003cmeta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n    \u003ctitle\u003eSSE\u003c/title\u003e\n    \n    \u003cscript src=\"https://cdn.jsdelivr.net/npm/jquery@3.6.1/dist/jquery.min.js\"\u003e\u003c/script\u003e\n    \u003clink rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/fomantic-ui@2.9.0/dist/semantic.min.css\"\u003e\n    \u003cscript src=\"https://cdn.jsdelivr.net/npm/fomantic-ui@2.9.0/dist/semantic.min.js\"\u003e\u003c/script\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003cdiv style=\"height: 100vh\" class=\"ui inverted segment\"\u003e\n        \u003cdiv class=\"ui header\"\u003eList\u003c/div\u003e\n        \u003cul id=\"items\"\u003e\n            \n        \u003c/ul\u003e\n    \u003c/div\u003e\n\u003cscript\u003e\nlet eventStream = new EventSource(\"http://localhost:8000/sse\");\n\neventStream.addEventListener(\"item\", (event) =\u003e {\n    $(\"#items\").append(`\u003cli\u003e${event.data}\u003c/li\u003e`);\n    console.log(event);\n});\n\neventStream.addEventListener(\"open\", () =\u003e {\n    console.log(\"connected\");\n    $.toast({\n        \"title\": \"Connected to SSE server\"\n    });\n})\n\u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommandstring%2Freactphp-sse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcommandstring%2Freactphp-sse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommandstring%2Freactphp-sse/lists"}