{"id":14984079,"url":"https://github.com/cydrickn/swoolewebsocketbundle","last_synced_at":"2025-06-22T03:38:04.513Z","repository":{"id":57846812,"uuid":"528424036","full_name":"cydrickn/SwooleWebsocketBundle","owner":"cydrickn","description":"A bundle for websocket using swoole","archived":false,"fork":false,"pushed_at":"2022-09-30T17:14:08.000Z","size":28,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T17:21:25.755Z","etag":null,"topics":["openswoole","runtime","swoole","symfony","symfony-bundle","symfony-runtime","websocket"],"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/cydrickn.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":"2022-08-24T12:55:38.000Z","updated_at":"2023-07-08T22:29:33.000Z","dependencies_parsed_at":"2022-09-19T13:01:02.887Z","dependency_job_id":null,"html_url":"https://github.com/cydrickn/SwooleWebsocketBundle","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cydrickn%2FSwooleWebsocketBundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cydrickn%2FSwooleWebsocketBundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cydrickn%2FSwooleWebsocketBundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cydrickn%2FSwooleWebsocketBundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cydrickn","download_url":"https://codeload.github.com/cydrickn/SwooleWebsocketBundle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248281424,"owners_count":21077423,"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":["openswoole","runtime","swoole","symfony","symfony-bundle","symfony-runtime","websocket"],"created_at":"2024-09-24T14:08:24.937Z","updated_at":"2025-04-10T19:43:26.924Z","avatar_url":"https://github.com/cydrickn.png","language":"PHP","readme":"# Swoole Websocket Bundle\n\nA bundle for websocket using swoole\n\n## Requirements\n\n- [Swoole](https://www.swoole.com/) or [OpenSwoole](https://openswoole.com/)\n- PHP \u003e= 8.1\n- Symfony \u003e= 6.1\n\n## Intallation\n\nAdd the package\n```shell\ncomposer require cydrickn/swoole-websocket-bundle\n```\n\n## Run the server\n\n```shell\nphp ./bin/console websocket:server\n```\n\n## Commands\n\n### websocket:server\n\nRun the websocket server\n\n| Options | Details                | Default   |\n|---------|------------------------|-----------|\n| host    | The host of the server | 127.0.0.1 |\n| port    | The port of the server | 8000      |\n\n## Events\n\nThis bundle will have an event where you can listen\n\n### \\Cydrickn\\SwooleWebsocketBundle\\Event\\OpenEvent\n\nThis event will be trigger once a client have been connected to the server.\n\n### \\Cydrickn\\SwooleWebsocketBundle\\Event\\MessageEvent\n\nThis event will be trigger once the client send a message\n\n### \\Cydrickn\\SwooleWebsocketBundle\\Event\\CloseEvent\n\nThis event will be trigger once a client have been disconnected\n\n## Client connection using Javascript\n\nFor the basic way on connecting to websocket you can use the Browser Websocket\n\n```js\nconst socket = new WebSocket('ws://localhost:8080');\n\n// Connection opened\nsocket.addEventListener('open', (event) =\u003e {\n    socket.send('Hello Server!');\n});\n\n// Listen for messages\nsocket.addEventListener('message', (event) =\u003e {\n    console.log('Message from server ', event.data);\n});\n\n```\n\n## Use it in runtime\n\nUsing the command is fine, but if you happen you want to serve this using symfony runtime you can do so.\n\n1. First you need to include symfony/runtime\n   ```shell\n   composer require symfony/runtime\n   ```\n2. Next run your `public/index.php` with env APP_RUNTIME\n   ```shell\n   APP_RUNTIME=\\\\Cydrickn\\\\SwooleWebsocketBundle\\\\Runtime\\\\Runtime php ./public/index.php\n   ```\n\n#### Runtime Configuration\n\nFor runtime configuration you can add it to your `public/index.php`\n\n| Key        | Details                                                                                                         | Default             |\n|------------|-----------------------------------------------------------------------------------------------------------------|---------------------|\n| host       | The host of the server                                                                                          | 127.0.0.1           |\n| port       | The port of the server                                                                                          | 8000                |\n| mode       | The mode for server                                                                                             | 2 / SWOOLE_PROCESS  |\n| sock_type  | The socket type for server                                                                                      | 1 / SWOOLE_SOCK_TCP |\n| settings   | The setting is base on [swoole configuration ](https://openswoole.com/docs/modules/swoole-server/configuration) | [] / Empty Array    |\n| serve_http | Include to serve HTTP                                                                                           | false               |\n\n**Example**:\n```php\n#!/usr/bin/env php\n\u003c?php\n\nuse App\\Kernel;\n\n$_SERVER['APP_RUNTIME_OPTIONS'] = [\n    'host' =\u003e '0.0.0.0',\n    'port' =\u003e 8000,\n    'mode' =\u003e SWOOLE_PROCESS,\n    'settings' =\u003e [\n        \\Swoole\\Constant::OPTION_WORKER_NUM =\u003e swoole_cpu_num() * 2,\n        \\Swoole\\Constant::OPTION_ENABLE_STATIC_HANDLER =\u003e true,\n        \\Swoole\\Constant::OPTION_DOCUMENT_ROOT =\u003e dirname(__DIR__).'/public'\n    ],\n];\n\nrequire_once dirname(__DIR__).'/vendor/autoload_runtime.php';\n\nreturn function (array $context) {\n    return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);\n};\n```\n\n\u003e Also instead of having to server for websocket and http, you can just enable the `Serve HTTP`, by setting the `serve_http` to true\n\n## Hot reloading\n\nWe already include a hor reloading for this bundle.\nTo enable it you need to first require [cydrickn/php-watcher](https://packagist.org/packages/cydrickn/php-watcher)\n\nHot reloading is only available if you are using runtime\n\n```shell\ncomposer require cydrickn/php-watcher\n```\n\nNow add to your `APP_RUNTIME_OPTIONS` the `hotReload`\nThe `basePath` should be your project folder\n\n```php\n$_SERVER['APP_RUNTIME_OPTIONS'] = [\n    'host' =\u003e '0.0.0.0',\n    'port' =\u003e 8000,\n    'mode' =\u003e SWOOLE_PROCESS,\n    'hotReload'=\u003e [\n        'enabled' =\u003e true,\n        'basePath' =\u003e dirname(__DIR__),\n    ],\n    'settings' =\u003e [\n        \\Swoole\\Constant::OPTION_WORKER_NUM =\u003e swoole_cpu_num() * 2,\n        \\Swoole\\Constant::OPTION_ENABLE_STATIC_HANDLER =\u003e true,\n    ],\n];\n```\n\n## Socket IO\n\nIn this bundle you can also enable using socket.io implementation, to enable it first you need to install\n[cydrickn/socketio](https://packagist.org/packages/cydrickn/socketio)\n\nThis implementation is only available if you are using runtime\n\n```shell\ncomposer require cydrickn/socketio\n```\n\nNow add to your `APP_RUNTIME_OPTIONS` set the `socketio` to `true`\n\n```php\n$_SERVER['APP_RUNTIME_OPTIONS'] = [\n    'socketio' =\u003e true,\n    'host' =\u003e '0.0.0.0',\n    'port' =\u003e 8000,\n    'mode' =\u003e SWOOLE_PROCESS,\n    'settings' =\u003e [\n        \\Swoole\\Constant::OPTION_WORKER_NUM =\u003e swoole_cpu_num() * 2,\n        \\Swoole\\Constant::OPTION_ENABLE_STATIC_HANDLER =\u003e true,\n    ],\n];\n```\n\n### Adding a route to socket io\n\nFor pure socket io you will do this\n```php\n$server-\u003eon('connection', function (\\Cydrickn\\SocketIO\\Socket $socket) {\n    $socket-\u003eemit('hello', 'world');\n});\n\n$server-\u003eon('chat', function (\\Cydrickn\\SocketIO\\Socket $socket, $message) {\n    $socket-\u003ebroadcast()-\u003eemit('chat', $message);\n});\n```\n\nYou will use the Attribute Route of this bundle `Cydrickn\\SwooleWebsocketBundle\\Attribute\\RouteAttribute`\nIn any of your service\n\n```php\n\u003c?php\nnamespace App\\Controller;\n\nuse Cydrickn\\SwooleWebsocketBundle\\Attribute\\RouteAttribute;\n\nclass SocketController\n{\n   #[RouteAttribute(path: 'connection')]\n   public function connection(\\Cydrickn\\SocketIO\\Socket $socket)\n   {\n      $socket-\u003eemit('hello', 'world');\n   }\n   \n   #[RouteAttribute(path: 'chat')]\n   public function anotherMethod(\\Cydrickn\\SocketIO\\Socket $socket, $message)\n   {\n      $socket-\u003ebroadcast()-\u003eemit('chat', $message);\n   }\n}\n```\n\n## Related Package\n\n- [PHP Watcher](https://github.com/cydrickn/php-watcher)\n- [PHP Socket IO](https://github.com/cydrickn/SocketIO)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcydrickn%2Fswoolewebsocketbundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcydrickn%2Fswoolewebsocketbundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcydrickn%2Fswoolewebsocketbundle/lists"}