phuongdev89/yii2-socketio 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phuongdev89/yii2-socketio

Composer 安装命令:

composer require phuongdev89/yii2-socketio

包简介

The simple and powerful socketio for the Yii2 framework

README 文档

README

Use all power of socket.io in your Yii 2 project.

Install

Install nodejs (tested with node 18)
    curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
    sudo apt-get install -y nodejs
Add to composer.json
{
    "require" : {
        "phuongdev89/yii2-socketio": "^2"
    },
    ...
    "scripts" : {
        "post-install-cmd": "cd vendor/phuongdev89/yii2-socketio && /usr/bin/npm install",
        "post-update-cmd": "cd vendor/phuongdev89/yii2-socketio && /usr/bin/npm install"
    }
}

Config

Console config (simple fork)

    'controllerMap' => [
        'socketio' => [
            'class' => \phuongdev89\socketio\commands\SocketIoCommand::class,
            'server' => 'localhost:1367'
        ],
    ]       
Start sockeio server
    php yii socketio/start
Stop sockeio server
    php yii socketio/stop
Common config
    'components' =>[
        'broadcastEvent' => [
            'class' => \phuongdev89\socketio\components\BroadcastEvent::class,
            'nsp' => 'some_unique_key', //must be changed
            // Namespaces with events folders
            'namespaces' => [
                'app\socketio',
            ]
        ],
        'broadcastDriver' => [
            'class' => \phuongdev89\socketio\components\BroadcastDriver::class,
            'hostname' => 'localhost',
            'port' => 6379,
        ],    
    ]

Usage

Publisher

Create publisher from server to client

    use phuongdev89\socketio\events\EventInterface;
    use phuongdev89\socketio\events\EventPubInterface;
    
    class CountEvent implements EventInterface, EventPubInterface
    {
        /**
         * Channel name. For client side this is nsp.
         */
        public static function broadcastOn(): array
        {
            return ['notifications'];
        }
    
        /**
         * Event name
         */
        public static function name(): string
        {
            return 'update_notification_count';
        }
            
        /**
         * Emit client event
         * @param array $data
         * @return array
         */
        public function fire(array $data): array
        {
            return $data;
        }
    }

On client using socketio to receive data from server

    var socket = io('localhost:1367/notifications');
    socket.on('update_notification_count', function(data){
        console.log(data)
    });

Using to broadcast data to client

    //Run broadcast to client
    \phuongdev89\socketio\Broadcast::emit(CountEvent::name(), ['count' => 10]);

Receiver

Create receiver from client to server

    use phuongdev89\socketio\events\EventInterface;
    use phuongdev89\socketio\events\EventSubInterface;
    
    class MarkAsReadEvent implements EventInterface, EventSubInterface
    {
        /**
         * Changel name. For client side this is nsp.
         */
        public static function broadcastOn(): array
        {
            return ['notifications'];
        }
    
        /**
         * Event name
         */
        public static function name(): string
        {
            return 'mark_as_read_notification';
        }
            
        /**
         * Emit client event
         * @param array $data
         * @return array
         */
        public function handle(array $data)
        {
            // Mark notification as read
            // And call client update
            file_put_contents(\Yii::getAlias('@app/file.txt'), json_encode($data));
        }
    }

On client using socketio to emit data to server

    var socket = io('localhost:1367/notifications');
    socket.emit('mark_as_read_notification', {id: 10});

Receiver with checking from client to server

You can have publisher and receiver in one event. If you need check data from client to server you should use:

  • EventPolicyInterface
    use phuongdev89\socketio\events\EventSubInterface;
    use phuongdev89\socketio\events\EventInterface;
    use phuongdev89\socketio\events\EventPolicyInterface;
    
    class MarkAsReadEvent implements EventInterface, EventSubInterface, EventPolicyInterface
    {
        /**
         * Changel name. For client side this is nsp.
         */
        public static function broadcastOn(): array
        {
            return ['notifications'];
        }
    
        /**
         * Event name
         */
        public static function name(): string
        {
            return 'mark_as_read_notification';
        }
         
        /**
        * @param $data
        * @return bool
        */
        public function can($data): bool
        {
            // Check data from client    
            return true;
        }        
        
        /**
         * Emit client event
         * @param array $data
         * @return array
         */
        public function handle(array $data)
        {
            // Mark notification as read
            // And call client update
            file_put_contents(\Yii::getAlias('@app/file.txt'), json_encode($data));
        }
    }

Subscribe room

Socket.io has room function. If you need it, you should implement EventRoomInterface

    use phuongdev89\socketio\events\EventPubInterface;
    use phuongdev89\socketio\events\EventInterface;
    use phuongdev89\socketio\events\EventRoomInterface;
    
    class CountEvent implements EventInterface, EventPubInterface, EventRoomInterface
    {
        /**
         * User id
         * @var int
         */
        protected $user_id;
        
        /**
         * Channel name. For client side this is nsp.
         */
        public static function broadcastOn(): array
        {
            return ['notifications'];
        }
    
        /**
         * Event name
         */
        public static function name(): string
        {
            return 'update_notification_count';
        }
           
        /**
         * Socket.io room
         * @return string
         */
        public function room(): string
        {
            return 'user_id_' . $this->user_id;
        }            
            
        /**
         * Emit client event
         * @param array $data
         * @return array
         */
        public function fire(array $data): array
        {
            $this->user_id = $data['user_id'];
            return [
                'count' => 10,
            ];
        }
    }

Subscribe room with event

You should use trait ListenTrait

    use phuongdev89\socketio\events\EventPubInterface;
    use phuongdev89\socketio\events\EventInterface;
    use phuongdev89\socketio\events\EventRoomInterface;
    use phuongdev89\socketio\traits\ListenTrait;
    
    class CountEvent implements EventInterface, EventPubInterface, EventRoomInterface
    {
        use ListenTrait;
        /**
         * User id
         * @var int
         */
        protected $userId;
        
        /**
         * Channel name. For client side this is nsp.
         */
        public static function broadcastOn(): array
        {
            return ['notifications'];
        }
    
        /**
         * Socket.io room
         * @return string
         */
        public function room(): string
        {
            return 'user_id_' . $this->userId;
        }            
            
        /**
         * Emit client event
         * @param array $data
         * @return array
         */
        public function fire(array $data): array
        {
            $this->userId = $data['userId'];
            return [
                'count' => 10,
            ];
        }
        
        public function handle(array $data)
        {
            $this->listen($data); //must place before your code
            file_put_contents(\Yii::getAlias('@app/../file.txt'), serialize($data));
        }
        
        public function onLeave($room_id)
        {
         // TODO: Implement onLeave() method.
        }
        
        public function onDisconnect($room_id)
        {
         // TODO: Implement onDisconnect() method.
        }
        
        public function onJoin($room_id)
        {
         // TODO: Implement onJoin() method.
        }
    }

On client using socketio to join the room, and listen data

    var socket = io('localhost:1367/notifications');
    socket.emit('join', {room: 'user_id_10'});
    // Now you will receive data from 'room-10'
    socket.on('update_notification_count', function(data){
        console.log(data)
    });
    // You can leave room
    socket.emit('leave');

Using to broadcast data to client on the room

    //Run broadcast to user id = 10 
    \phuongdev89\socketio\Broadcast::emitToRoom(CountEvent::class, [
        'count' => 4, 
        'user_id' => 10,//push data to room-10
    ]);

phuongdev89/yii2-socketio 适用场景与选型建议

phuongdev89/yii2-socketio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 87 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 phuongdev89/yii2-socketio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phuongdev89/yii2-socketio 我们能提供哪些服务?
定制开发 / 二次开发

基于 phuongdev89/yii2-socketio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 87
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 32
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-10