{"id":13828568,"url":"https://github.com/yiicod/yii2-socketio","last_synced_at":"2025-05-07T04:06:13.639Z","repository":{"id":45709340,"uuid":"92400317","full_name":"yiicod/yii2-socketio","owner":"yiicod","description":null,"archived":false,"fork":false,"pushed_at":"2023-07-07T07:20:17.000Z","size":34,"stargazers_count":46,"open_issues_count":4,"forks_count":31,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-07T04:06:02.340Z","etag":null,"topics":["php","socket","socket-io","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yiicod.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2017-05-25T12:15:51.000Z","updated_at":"2024-05-19T21:04:01.000Z","dependencies_parsed_at":"2024-08-04T09:08:45.379Z","dependency_job_id":"6c3bc397-e998-4466-b9ad-272c5bea4a24","html_url":"https://github.com/yiicod/yii2-socketio","commit_stats":{"total_commits":32,"total_committers":4,"mean_commits":8.0,"dds":0.28125,"last_synced_commit":"b4146bb8d8054192eacba1a94e7ed78605eeb7bc"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiicod%2Fyii2-socketio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiicod%2Fyii2-socketio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiicod%2Fyii2-socketio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiicod%2Fyii2-socketio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yiicod","download_url":"https://codeload.github.com/yiicod/yii2-socketio/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252810273,"owners_count":21807759,"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":["php","socket","socket-io","yii2","yii2-extension"],"created_at":"2024-08-04T09:02:52.749Z","updated_at":"2025-05-07T04:06:13.619Z","avatar_url":"https://github.com/yiicod.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"Socket.io Yii extension\n=======================\n\nUse all power of socket.io in your Yii 2 project.\n\n[![Latest Stable Version](https://poser.pugx.org/yiicod/yii2-socketio/v/stable)](https://packagist.org/packages/yiicod/yii2-socketio) [![Total Downloads](https://poser.pugx.org/yiicod/yii2-socketio/downloads)](https://packagist.org/packages/yiicod/yii2-socketio) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yiicod/yii2-socketio/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yiicod/yii2-socketio/?branch=master)[![Code Climate](https://codeclimate.com/github/yiicod/yii2-socketio/badges/gpa.svg)](https://codeclimate.com/github/yiicod/yii2-socketio)\n\nConfig\n------\n\n##### Install node + additional npm\n```bash\n    cd ~\n    curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh\n    sudo bash nodesource_setup.sh\n    cd vendor/yiicod/yii2-socketio/server\n    npm install\n```\n\n#### Console config (simple fork)\n```php\n    'controllerMap' =\u003e [\n        'socketio' =\u003e [\n            'class' =\u003e \\yiicod\\socketio\\commands\\SocketIoCommand::class,\n            'server' =\u003e 'localhost:1367'\n        ],\n    ]       \n```\n###### Start sockeio server\n```bash\n    php yii socketio/start\n```\n###### Stop sockeio server\n```bash\n    php yii socketio/stop\n```\n#### Console config + PM2(http://pm2.keymetrics.io/). This variant more preferable for console configuration\n```php\n    'controllerMap' =\u003e [\n        'socketio' =\u003e [\n            'class' =\u003e \\yiicod\\socketio\\commands\\WorkerCommand::class,\n            'server' =\u003e 'localhost:1367'\n        ],\n    ]\n```\n###### pm2 config:\n```json\n    {\n      \"apps\": [\n        {\n          \"name\": \"socket-io-node-js-server\",\n          \"script\": \"yii\",\n          \"args\": [\n            \"socketio/node-js-server\"\n          ],\n          \"exec_interpreter\": \"php\",\n          \"exec_mode\": \"fork_mode\",\n          \"max_memory_restart\": \"1G\",\n          \"watch\": false,\n          \"merge_logs\": true,\n          \"out_file\": \"runtime/logs/node_js_server_out.log\",\n          \"error_file\": \"runtime/logs/node_js_server_err.log\"\n        },\n        {\n          \"name\": \"socket-io-php-server\",\n          \"script\": \"yii\",\n          \"args\": [\n            \"socketio/php-server\"\n          ],\n          \"exec_interpreter\": \"php\",\n          \"exec_mode\": \"fork_mode\",\n          \"max_memory_restart\": \"1G\",\n          \"watch\": false,\n          \"merge_logs\": true,\n          \"out_file\": \"runtime/logs/php_server_out.log\",\n          \"error_file\": \"runtime/logs/php_server_err.log\"\n        },\n      ]\n    }\n```\n###### Run PM2 daemons\n```bash\npm2 start daemons-app.json\n```\n###### PM2 will be run these two commands in background::\n```bash\n    php yii socketio/node-js-server\n    php yii socketio/php-server\n```\n\n##### Common config\n```php\n    'components' =\u003e[\n        'broadcastEvents' =\u003e [\n            'class' =\u003e \\yiicod\\socketio\\EventManager::class,\n            'nsp' =\u003e 'some_unique_key',\n            // Namespaces with events folders\n            'namespaces' =\u003e [\n                'app\\socketio',\n            ]\n        ],\n        'broadcastDriver' =\u003e [\n            'class' =\u003e \\yiicod\\socketio\\drivers\\RedisDriver::class,\n            'hostname' =\u003e 'localhost',\n            'port' =\u003e 6379,\n        ],    \n    ]\n```\n\n##### Create publisher from server to client\n```php\n    use yiicod\\socketio\\events\\EventInterface;\n    use yiicod\\socketio\\events\\EventPubInterface;\n    \n    class CountEvent implements EventInterface, EventPubInterface\n    {\n        /**\n         * Channel name. For client side this is nsp.\n         */\n        public static function broadcastOn(): array\n        {\n            return ['notifications'];\n        }\n    \n        /**\n         * Event name\n         */\n        public static function name(): string\n        {\n            return 'update_notification_count';\n        }\n            \n        /**\n         * Emit client event\n         * @param array $data\n         * @return array\n         */\n        public function fire(array $data): array\n        {\n            return $data;\n        }\n    }\n```\n```js\n    var socket = io('localhost:1367/notifications');\n    socket.on('update_notification_count', function(data){\n        console.log(data)\n    });\n```\n```php\n    //Run broadcast to client\n    \\yiicod\\socketio\\Broadcast::emit(CountEvent::name(), ['count' =\u003e 10])\n\n```\n\n##### Create receiver from client to server\n```php\n    use yiicod\\socketio\\events\\EventInterface;\n    use yiicod\\socketio\\events\\EventSubInterface;\n    \n    class MarkAsReadEvent implements EventInterface, EventSubInterface\n    {\n        /**\n         * Channel name. For client side this is nsp.\n         */\n        public static function broadcastOn(): array\n        {\n            return ['notifications'];\n        }\n    \n        /**\n         * Event name\n         */\n        public static function name(): string\n        {\n            return 'mark_as_read_notification';\n        }\n            \n        /**\n         * Emit client event\n         * @param array $data\n         * @return array\n         */\n        public function handle(array $data)\n        {\n            // Mark notification as read\n            // And call client update\n            // Broadcast::emit('update_notification_count', ['some_key' =\u003e 'some_value']);\n            \n            // Push some log\n            file_put_contents(\\Yii::getAlias('@app/../file.txt'), serialize($data));\n        }\n    }\n```\n```js\n    var socket = io('localhost:1367/notifications');\n    socket.emit('mark_as_read_notification', {id: 10});\n```\n\nYou can have publisher and receiver in one event. If you need check data from client to server you should use: \n- EventPolicyInterface\n\n##### Receiver with checking from client to server\n```php\n    use yiicod\\socketio\\events\\EventSubInterface;\n    use yiicod\\socketio\\events\\EventInterface;\n    use yiicod\\socketio\\events\\EventPolicyInterface;\n    \n    class MarkAsReadEvent implements EventInterface, EventSubInterface, EventPolicyInterface\n    {\n        /**\n         * Channel name. For client side this is nsp.\n         */\n        public static function broadcastOn(): array\n        {\n            return ['notifications'];\n        }\n    \n        /**\n         * Event name\n         */\n        public static function name(): string\n        {\n            return 'mark_as_read_notification';\n        }\n         \n        public function can($data): bool\n        {\n            // Check data from client    \n            return true;\n        }        \n        \n        /**\n         * Emit client event\n         * @param array $data\n         * @return array\n         */\n        public function handle(array $data)\n        {\n            // Mark notification as read\n            // And call client update\n            Broadcast::emit('update_notification_count', ['some_key' =\u003e 'some_value']);\n        }\n    }\n```\n\nSoket.io has room functionl. If you need it, you should implement:\n- EventRoomInterface\n```php\n    use yiicod\\socketio\\events\\EventPubInterface;\n    use yiicod\\socketio\\events\\EventInterface;\n    use yiicod\\socketio\\events\\EventRoomInterface;\n    \n    class CountEvent implements EventInterface, EventPubInterface, EventRoomInterface\n    {\n        /**\n         * User id\n         * @var int\n         */\n        protected $userId;\n        \n        /**\n         * Channel name. For client side this is nsp.\n         */\n        public static function broadcastOn(): array\n        {\n            return ['notifications'];\n        }\n    \n        /**\n         * Event name\n         */\n        public static function name(): string\n        {\n            return 'update_notification_count';\n        }\n           \n        /**\n         * Socket.io room\n         * @return string\n         */\n        public function room(): string\n        {\n            return 'user_id_' . $this-\u003euserId;\n        }            \n            \n        /**\n         * Emit client event\n         * @param array $data\n         * @return array\n         */\n        public function fire(array $data): array\n        {\n            $this-\u003euserId = $data['userId'];\n            return [\n                'count' =\u003e 10,\n            ];\n        }\n    }\n```\n```js\n    var socket = io('localhost:1367/notifications');\n    socket.emit('join', {room: 'user_id_'\u003c?= 10 ?\u003e});\n    // Now you will receive data from 'room-1'\n    socket.on('update_notification_count', function(data){\n        console.log(data)\n    });\n    // You can leave room\n    socket.emit('leave');\n```\n```php\n    //Run broadcast to user id = 10 \n    \\yiicod\\socketio\\Broadcast::emit(CountEvent::name(), ['count' =\u003e 10, 'userId' =\u003e 10])\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiicod%2Fyii2-socketio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyiicod%2Fyii2-socketio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiicod%2Fyii2-socketio/lists"}