{"id":19436655,"url":"https://github.com/cydrickn/socketio","last_synced_at":"2025-09-01T12:04:59.719Z","repository":{"id":58922390,"uuid":"534552079","full_name":"cydrickn/SocketIO","owner":"cydrickn","description":"PHP Websocket Server that is compatible with socket.io","archived":false,"fork":false,"pushed_at":"2024-06-22T03:24:03.000Z","size":76,"stargazers_count":27,"open_issues_count":1,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-25T04:46:26.683Z","etag":null,"topics":["openswoole","php","server","socket","socket-io","socketio","swoole","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-09T07:57:31.000Z","updated_at":"2025-04-11T13:22:24.000Z","dependencies_parsed_at":"2023-02-14T05:31:45.017Z","dependency_job_id":"4afa0a49-7629-4c1b-8a02-2ebe4078274f","html_url":"https://github.com/cydrickn/SocketIO","commit_stats":{"total_commits":32,"total_committers":2,"mean_commits":16.0,"dds":0.03125,"last_synced_commit":"24db935a12ac4966f531765497f0f8bae0cad24b"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"purl":"pkg:github/cydrickn/SocketIO","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cydrickn%2FSocketIO","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cydrickn%2FSocketIO/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cydrickn%2FSocketIO/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cydrickn%2FSocketIO/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cydrickn","download_url":"https://codeload.github.com/cydrickn/SocketIO/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cydrickn%2FSocketIO/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273121703,"owners_count":25049530,"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","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","php","server","socket","socket-io","socketio","swoole","websocket"],"created_at":"2024-11-10T15:12:13.283Z","updated_at":"2025-09-01T12:04:59.672Z","avatar_url":"https://github.com/cydrickn.png","language":"PHP","readme":"# PHP SocketIO Server\nPHP Websocket Server that is compatible with [socket.io](https://socket.io/)\n\nSo far the function use in this package is almost same with the naming in socket.io\n\n## Installation\n\n```shell\ncomposer require cydrickn/socketio\n```\n\nThen in your main php code add\n\n```php\nrequire __DIR__ . '/../vendor/autoload.php';\n```\n\n## Usage\n\n### Initializing your server\n\nTo initialize \n\n```php\n$server = new \\Cydrickn\\SocketIO\\Server([\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$server-\u003eon('Started', function (\\Cydrickn\\SocketIO\\Server $server) {\n    echo 'Websocket is now listening in ' . $server-\u003egetHost() . ':' . $server-\u003egetPort() . PHP_EOL;\n});\n\n$server-\u003eon('connection', function (\\Cydrickn\\SocketIO\\Socket $socket) {\n    // ...\n});\n\n$server-\u003estart();\n```\n\n### Server Options\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\n## Server Instance\n\nThis is the server [\\Cydrickn\\SocketIO\\Server](src/Server.php), it also inherited all methods of [\\Cydrickn\\SocketIO\\Socket](src/Socket.php)\n\n## Events\n\n### Basic Emit\n\nTo emit, just need to call the `\\Cydrickn\\SocketIO\\Socket::emit`\n\n```php\n$server-\u003eon('connection', function (\\Cydrickn\\SocketIO\\Socket $socket) {\n    $socket-\u003eemit('hello', 'world');\n});\n```\n\nYou can also pass as many as you want for the parameters\n\n```php\n$socket-\u003eemit('hello', 'world', 1, 2, 3, 'more');\n```\n\nThere is no need to run json_encode on objects/arrays as it will be done for you.\n\n```php\n// BAD\n$socket-\u003eemit('hi', json_encode(['name' =\u003e 'Juan']));\n\n// GOOD\n$socket-\u003eemit('hi', ['name' =\u003e 'Juan']);\n```\n\n### Broadcasting\n\nBroadcast is like just the simple emit, but it will send to all connected client except the current client\n\n```php\n$socket-\u003ebroadcast()-\u003eemit('hi', ['name' =\u003e 'Juan']);\n```\n\n### Sending to all clients\n\nThe toAll will emit a message to all connected clients including the current client\n\n```php\n$socket-\u003etoAll()-\u003eemit('hi', ['name' =\u003e 'Juan']);\n```\n\n### Acknowledgements\n\nSame with the socket io for nodejs,\nyou just need to add a callable as the last argument in the emit\n\nServer\n```php\n$socket-\u003eemit('hi', function () {\n    //...\n});\n```\n\nClient\n```js\nsocket.on('hi', (callback) =\u003e {\n    callback('hello');\n});\n```\n\n#### With Timeout\n\nAssign a timeout to each emit:\n\n```php\n// timeout for 5 seconds\n$socket-\u003etimeout(5000)-\u003eemit('hi', function (bool $err) {\n    if ($err) {\n        // the other side did not acknowledge the event in the given delay\n    }\n});\n```\n\nAdd callback default arguments value for timeout\n\n```php\n// timeout for 5 seconds\n$socket-\u003etimeout(5000, 'Juan')-\u003eemit('hi', function (bool $err, string $name) {\n    var_dump($name); // if the other side did not acknowledge the event the $name will be 'Juan'\n    if ($err) {\n        // the other side did not acknowledge the event in the given delay\n    }\n});\n```\n\n### Listening\n\nTo listen to any event\n\n```php\n$server-\u003eon('hello', function (\\Cydrickn\\SocketIO\\Socket $socket, string $world) {\n    // ...\n});\n\n$server-\u003eon('hi', function (\\Cydrickn\\SocketIO\\Socket $socket, array $name) {\n    // ...\n});\n```\n\n## Server Event\n\nThis event can't be use for the route\nSince this event are Swoole Websocket Event\nAll Event with * should not be used\n\n- Request - When you use it for http\n- *WorkerStart - You must not replace this event since this is the worker start logic for this package\n- *Start - You must not replace this event since this is the start logic for this package\n- *Open - You must not replace this event since this is the connection logic for this package\n- *Message - You must not replace this event since this is the message logic for this package\n- *Close - You must not replace this event since this is the message logic for this package\n\n## Rooms\n\nIn this package it was already included the rooms\n\nTo join to group just call the function `join`\n\n```php\n$socket-\u003ejoin('room1');\n```\n\nTo emit a message\n\n```php\n$socket-\u003eto('room1')-\u003eemit('hi', ['name' =\u003e 'Juan']);\n```\n\nEmit to multiple room\n\n```php\n$socket-\u003eto('room1')-\u003eto('room2')-\u003eemit('hi', ['name' =\u003e 'Juan']);\n```\n\nLeaving the room\n\n```php\n$socket-\u003eleave('room1');\n```\n\nSending to specific user\n\nIn socket.io javascript, the user was automatically created a new room for each client sid.\n\nBut currently in this package it will not create a new room for each client.\n\nIn this package you just need to specify if its a room or a sid\n\n```php\nuse Cydrickn\\SocketIO\\Message\\Response;\n\n$socket-\u003eon('private message', (\\Cydrickn\\SocketIO\\Socket $socket, $anotherSocketId, $msg) =\u003e {\n    $socket-\u003e($anotherSocketId, Response::TO_TYPE_SID).emit('private message', $socket-\u003esid, $msg);\n});\n```\n\n## Middleware\n\nYou can add an middleware for the server\n\n```php\n$server-\u003euse(function (\\Cydrickn\\SocketIO\\Socket $socket, callable $next) {\n    // ...\n    $next();\n});\n```\n\nTo not continue the connection you just pass \\Error in the $next\n```php\n$server-\u003euse(function (\\Cydrickn\\SocketIO\\Socket $socket, callable $next) {\n    // ...\n    $next(new \\Error('Something went wrong'));\n});\n```\n\nYou can also add middleware for handshake event of Swoole Server.\nJust passed true to the second argument.\n\nAlso in callback it will pass the response for you to modify if you need it\n```php\n$server-\u003euse(function (\\Cydrickn\\SocketIO\\Socket $socket, \\Swoole\\Http\\Response $response, callable $next) {\n    // ...\n}, true);\n```\n\nIf you want to create a middleware as a class we recommend to implement the `Cydrickn\\SocketIO\\Middleware\\MiddlewareInterface`\nand if for handshake use `Cydrickn\\SocketIO\\Middleware\\HandshakeMiddlewareInterface`\n\nExample of middleware that use in handshake is the [Cydrickn\\SocketIO\\Middleware\\CookieSessionMiddleware](/src/Middleware/CookieSessionMiddleware.php).\nThis middleware will create a session that uses the cookie and if the client did not send the session cookie then it will\ncreate a cookie and response it from the handshake.\n\n## Session\n\nIn this package the there is already session storage that you can use,\n\n- **SessionsTable** - Uses the Swoole\\Table as the storage\n- **SessionsNative** - Uses the file storage\n\nUsing Swoole, session_start, $_SESSION should not be use since this function are global it stores the data\nin the process itself.\n\nThe session that provided here does not use this predefined session extensions.\nCurrently, the session is define per connection so you can take the session via.\n\n```php\n$socket-\u003egetSession();\n```\n\nThis getSession can return null if you don't have any middleware that creating the session.\n\nTo set a session\n```php\n$session = $server-\u003egetSessionStorage()-\u003eget('123456'); // This will automatically created once it does not exists\n$socket-\u003esetSession($session);\n```\n\nYou can also customize your session storage, just implement the\n[Cydrickn\\SocketIO\\Session\\SessionStorageInterface](src/Session/SessionStorageInterface.php)\n\n```php\n\u003c?php\n\nclass CustomeStorage implements SessionStorageInterface {\n    // ...\n}\n```\n\nAfter creating your storage\n\nYou need to pass this in your server constructor\n\n```php\n$server = new \\Cydrickn\\SocketIO\\Server([\n    'host' =\u003e '0.0.0.0',\n    'port' =\u003e 8000,\n    'mode' =\u003e SWOOLE_PROCESS,\n    'serve_http' =\u003e true,\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__).'/examples'\n    ]\n], sessionStorage: new CustomeStorage());\n```\n\n## Example\n\n- [Chat Example](https://github.com/cydrickn/SocketIO/tree/main/examples)\n\n## TODO\n\n- [X] Leaving room\n- [X] Fix disconnection event\n- [X] [Emit Acknowledgement](https://socket.io/docs/v4/emitting-events/#acknowledgements)\n- [ ] [Implement with timeout emit](https://socket.io/docs/v4/emitting-events/#with-timeout)\n- [ ] [Implement Catch all listeners](https://socket.io/docs/v4/listening-to-events/#catch-all-listeners)\n- [ ] [Implement once, off, removeAllListeners](https://socket.io/docs/v4/listening-to-events/#eventemitter-methods)\n\n\n## This package was used in\n\n- [Swoole Websocket Bundle - Symfony](https://github.com/cydrickn/SwooleWebsocketBundle)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcydrickn%2Fsocketio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcydrickn%2Fsocketio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcydrickn%2Fsocketio/lists"}