PHPackages                             xiaochengfu/yii2-swoole - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. xiaochengfu/yii2-swoole

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

xiaochengfu/yii2-swoole
=======================

yii2 Swoole 扩展，高度整合异步任务处理、websocket通信、服务端实时推送等服务

v1.0.3(8y ago)441455[3 issues](https://github.com/xiaochengfu/yii2-swoole/issues)BSD-3-ClausePHP

Since May 30Pushed 6y ago4 watchersCompare

[ Source](https://github.com/xiaochengfu/yii2-swoole)[ Packagist](https://packagist.org/packages/xiaochengfu/yii2-swoole)[ RSS](/packages/xiaochengfu-yii2-swoole/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

yii2 Swoole 扩展
==============

[](#yii2-swoole-扩展)

### 如仅需要websocket，或者需要支持laravel/thinkphp/yii2等框架，请移步最新扩展 [websocket-swoole](https://github.com/xiaochengfu/websocket-swoole) 不再受框架限制

[](#如仅需要websocket或者需要支持laravelthinkphpyii2等框架请移步最新扩展-websocket-swoole-不再受框架限制)

扩展内容主要包括: 一.异步任务队列 这里根据不同的需求,我设计了三种不同的异步处理方法

```
方法一(可用http请求投递):
Yii::$app->swoole->webTask($data);
    将异步任务,以触发浏览器链接的方式执行,适用于能通过web请求来处理的小耗时任务
    优点:操作简单
    缺点:安全性低,任务链接要做权限处理;
        稳定性较差,如果链接中有脚本错误,或连接超时,会导致任务丢失.

方法二(可用http请求投递):
Yii::$app->swoole->mongodbTask($data);
    将异步任务,投递到mongo中,由yii2的console来执行,用来处理耗时,重要的任务
    优点:稳定性好,任务队列记录到mongodb,可查找任务处理记录
    缺点:需要安装mongodb,配置mongo,操作较复杂

方法三(用cli请求投递):
Yii::$app->swoole->cliAsync($data);
    以cli的形式来投递任务,适用场景较多的为计划任务,来通过yii2的console来执行,同样由mongo来传递和记录任务队列

```

二.websocket通信 本扩展以websocket为基础服务,所以可以处理websocket的请求,多客户端连接通信,通过自定义命令来实时处理业务

三.基于websocket的实时推送 服务端有消息变更时,通过向客户端推送消息,来达到消息的同步和实时反馈``

```
Yii::$app->swoole->pushMsg($fd,$data);

```

四.简单的启动/关闭/重启/状态获取命令

swoole版本要求：&gt;=1.8.1

实现原理
----

[](#实现原理)

适用场景
----

[](#适用场景)

需要客户端触发的耗时请求，客户端无需等待返回结果 websocket的这种场景

安装
--

[](#安装)

```
composer require xiaochengfu/yii2-swoole "v1.0.3"

```

如何使用

安装前准备: 1.需要安装curl扩展,

```
composer require linslin/yii2-curl "1.1.3"

```

2.需要安装mongodb,因为有部分异步任务是需要存储到mongodb中

---

1、修改common/config/params.php

```
return [
    'swooleAsync' => [
        'host'             => 'ip', 		//服务启动IP
        'port'             => '9512',      		//服务启动端口
        'swoole_http'      => 'http://ip:9512',//推送触发连接地址
        'process_name'     => 'swooleWebSocket',		//服务进程名
        'open_tcp_nodelay' => '1',         		//启用open_tcp_nodelay
        'daemonize'        => false,				//守护进程化
        'heartbeat_idle_time' => 180,               //客户端向服务端请求的间隔时间,单位秒(s)
        'heartbeat_check_interval' => 120,          //服务端向客户端发送心跳包的间隔时间，两参数要配合使用,单位秒(s)
        'worker_num'       => '2',				//work进程数目
        'task_worker_num'  => '2',				//task进程的数量
        'task_max_request' => '10000',			//work进程最大处理的请求数
        'pidfile'           => Yii::getAlias('@swoole').'/yii2-swoole/yii2-swoole.pid',
        'log_dir'           => Yii::getAlias('@swoole').'/yii2-swoole',
        'task_tmpdir'       => Yii::getAlias('@swoole').'/yii2-swoole',
        'log_file'          => Yii::getAlias('@swoole').'/yii2-swoole/swoole.log',
        'log_size'          => 204800000,       //运行时日志 单个文件大小
    ]
];
```

2.上一步中,我把pidfile和log目录单独定义到了swoolelog目录下,如果你也采用相同的方法,你需要设置swoolelog别名，并创建swoolelog目录 修改common/bootstrap.php,添加如下内容:

```
Yii::setAlias('@swoole',dirname(dirname(__DIR__)) . '/swoolelog');

```

3、在console/config/main.php配置文件中增加controllerMap

```
 'controllerMap' => [
        'swoole' => [
            'class' => 'xiaochengfu\swoole\console\SwooleController',
        ],
        //test主要用来测试
        'test' => [
            'class' => 'xiaochengfu\swoole\console\TestController',
        ],
    ],
```

4、在主配置文件中增加components

```
'components' => [
     'swoole' => [
                 'class' => 'xiaochengfu\swoole\component\SwooleAsyncComponent',
             ]
]
```

5.在前端应用下（frontend/config/main.php）,添加modules，通过访问[http://ip/swoole来测试](http://ip/swoole%E6%9D%A5%E6%B5%8B%E8%AF%95)

```
 'modules' => [
        'swoole' => [
            'class' => 'xiaochengfu\swoole\Module',
        ]
    ],

```

6、服务管理

```
//启动
php /path/to/yii/application/yii swoole start

//重启
php /path/to/yii/application/yii swoole restart

//停止
php /path/to/yii/application/yii swoole stop

//查看状态
php /path/to/yii/application/yii swoole status

//查看进程列表
php /path/to/yii/application/yii swoole list

```

7、测试 a.通过分别访问front/swoole/default/index|mongo|del|来测试异步任务 b.通过访问front/swoole/default/push来测试websocket推送,fd为1,客户端需要自己建立连接, 网页在线测试客户端链接地址:`http://www.blue-zero.com/WebSocket/`c.通过执行

```
php /path/to/yii/application/yii test cli

```

来测试命令行的异步任务

大家如果有问题要交流，就发在这里吧： [\#1](https://github.com/xiaochengfu/yii2-swoole/issues/1)如有疑问：请加QQ：1033426413，验证信息：swoole接入，我会来交流区查看的

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~154 days

Total

2

Last Release

3116d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1a10742992d52a3b211ea0790e1db0e1eb0c4230296b34b290457fa520a8d4ef?d=identicon)[xiaochengfu](/maintainers/xiaochengfu)

---

Top Contributors

[![xiaochengfu](https://avatars.githubusercontent.com/u/15496255?v=4)](https://github.com/xiaochengfu "xiaochengfu (25 commits)")

---

Tags

swoole-taskswoole-websocketyii2-swooleswooleyii2extensionyii2-swoole

### Embed Badge

![Health badge](/badges/xiaochengfu-yii2-swoole/health.svg)

```
[![Health](https://phpackages.com/badges/xiaochengfu-yii2-swoole/health.svg)](https://phpackages.com/packages/xiaochengfu-yii2-swoole)
```

###  Alternatives

[vyants/yii2-daemon

Extension provides functionality for simple daemons creation and control

7859.0k](/packages/vyants-yii2-daemon)[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)[richardfan1126/yii2-js-register

Yii2 widget to register JS into view

1357.2k7](/packages/richardfan1126-yii2-js-register)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
