PHPackages                             topthink/think-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. [Framework](/categories/framework)
4. /
5. topthink/think-swoole

ActiveLibrary[Framework](/categories/framework)

topthink/think-swoole
=====================

Swoole extend for thinkphp

v4.1.2(11mo ago)477174.4k↑45.4%104[14 issues](https://github.com/top-think/think-swoole/issues)19Apache-2.0PHPPHP ^8.0

Since Oct 5Pushed 8mo ago20 watchersCompare

[ Source](https://github.com/top-think/think-swoole)[ Packagist](https://packagist.org/packages/topthink/think-swoole)[ RSS](/packages/topthink-think-swoole/feed)WikiDiscussions 4.0 Synced 1mo ago

READMEChangelog (10)Dependencies (11)Versions (57)Used By (19)

ThinkPHP Swoole 扩展
==================

[](#thinkphp-swoole-扩展)

交流群：787100169 [![点击加群](https://camo.githubusercontent.com/75b53e353bb9e5064662e185a6d39f4bb88c4e45bd3a1240ddf599525edb6afa/68747470733a2f2f7075622e69647171696d672e636f6d2f7770612f696d616765732f67726f75702e706e67 "点击加群")](https://jq.qq.com/?_wv=1027&k=VRcdnUKL)

安装
--

[](#安装)

首先按照Swoole官网说明安装swoole扩展，然后使用

```
composer require topthink/think-swoole

```

安装swoole扩展。

使用方法
----

[](#使用方法)

直接在命令行下启动HTTP服务端。

```
php think swoole

```

启动完成后，默认会在0.0.0.0:8080启动一个HTTP Server，可以直接访问当前的应用。

swoole的相关参数可以在`config/swoole.php`里面配置（具体参考配置文件内容）。

如果需要使用守护进程方式运行，建议使用supervisor来管理进程

访问静态文件
------

[](#访问静态文件)

> 4.0开始协程风格服务端默认不支持静态文件访问，建议使用nginx来支持静态文件访问，也可使用路由输出文件内容，下面是示例，可参照修改

1. 添加静态文件路由：

```
Route::get('static/:path', function (string $path) {
    $filename = public_path() . $path;
    return new \think\swoole\response\File($filename);
})->pattern(['path' => '.*\.\w+$']);
```

2. 访问路由 `http://localhost/static/文件路径`

队列支持
----

[](#队列支持)

> 4.0开始协程风格服务端没有task进程了，使用think-queue代替

使用方法见 [think-queue](https://github.com/top-think/think-queue)

以下配置代替think-queue里的最后一步:`监听任务并执行`,无需另外起进程执行队列

```
return [
    // ...
    'queue'      => [
        'enable'  => true,
        //键名是队列名称
        'workers' => [
            //下面参数是不设置时的默认配置
            'default'            => [
                'delay'      => 0,
                'sleep'      => 3,
                'tries'      => 0,
                'timeout'    => 60,
                'worker_num' => 1,
            ],
            //使用@符号后面可指定队列使用驱动
            'default@connection' => [
                //此处可不设置任何参数，使用上面的默认配置
            ],
        ],
    ],
    // ...
];
```

### websocket

[](#websocket)

> 新增路由调度的方式，方便实现多个websocket服务

#### 配置

[](#配置)

```
swoole.websocket.route = true 时开启

```

#### 路由定义

[](#路由定义)

```
Route::get('path1','controller/action1');
Route::get('path2','controller/action2');
```

#### 控制器

[](#控制器)

```
use \think\swoole\Websocket;
use \think\swoole\websocket\Event;
use \Swoole\WebSocket\Frame;
use \think\swoole\websocket\Room;

class Controller {

    public function action1(){//不可以在这里注入websocket对象

        return \think\swoole\helper\websocket()
            ->onOpen(...)
            ->onMessage(function(Websocket $websocket, Frame $frame){ //只可在事件响应这里注入websocket对象
                //...
                $websocket->join('room_key'); //将当前连接加入到某个room，后续可以向该room发送消息 这个room里的都可以收到
                //比如room_key可以直接使用这个用户的id，然后其他地方需要给某个用户发送消息，直接向这个room发送消息即可
                //...
                $websocket->push('message'); //给当前连接发送消息
                //...
                $websocket->emit('event_name', 'message'); //给当前连接发送事件
                //...
                $websocket->to('room_key')->push('message'); //给指定room的所有连接发送消息 在http请求的控制器中也可以注入Websocket对象这样发消息
                //...
            })
            ->onClose(...);
    }

    public function action2(){

        return \think\swoole\helper\websocket()
            ->onOpen(...)
            ->onMessage(function(Websocket $websocket, Frame $frame){
               //...
            })
            ->onClose(...);
    }
}
```

### 流式输出

[](#流式输出)

```
class Controller {

    public function action(){
        return \think\swoole\helper\iterator(value(function(){
            foreach(range(1,10) as $i)
                yield $i;
                sleep(1);//模拟等待
            }
        }));
    }
}
```

###  Health Score

61

—

FairBetter than 99% of packages

Maintenance55

Moderate activity, may be stable

Popularity56

Moderate usage in the ecosystem

Community40

Growing community involvement

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 59.2% 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 ~59 days

Recently: every ~70 days

Total

56

Last Release

256d ago

Major Versions

v2.0.17 → v3.0.02019-04-09

v2.0.18 → v3.0.102020-12-09

v3.1.3 → v4.0.02021-05-27

2.0.x-dev → v4.0.42021-07-08

3.0.x-dev → v4.0.82022-12-14

PHP version history (3 changes)v3.0.6PHP &gt;7.1

v4.0.0PHP &gt;=7.4

v4.0.10PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7858077?v=4)[topthink](/maintainers/topthink)[@topthink](https://github.com/topthink)

![](https://www.gravatar.com/avatar/7422243107bba2d58c5682b69d1c405e66fa027df2f6202afc1bb0c55559a7b1?d=identicon)[yunwuxin](/maintainers/yunwuxin)

---

Top Contributors

[![yunwuxin](https://avatars.githubusercontent.com/u/2168125?v=4)](https://github.com/yunwuxin "yunwuxin (267 commits)")[![liu21st](https://avatars.githubusercontent.com/u/1111670?v=4)](https://github.com/liu21st "liu21st (108 commits)")[![xavieryang007](https://avatars.githubusercontent.com/u/6174650?v=4)](https://github.com/xavieryang007 "xavieryang007 (32 commits)")[![xqueezeme](https://avatars.githubusercontent.com/u/97489679?v=4)](https://github.com/xqueezeme "xqueezeme (8 commits)")[![sy-records](https://avatars.githubusercontent.com/u/33931153?v=4)](https://github.com/sy-records "sy-records (8 commits)")[![HyperLife1119](https://avatars.githubusercontent.com/u/41798664?v=4)](https://github.com/HyperLife1119 "HyperLife1119 (8 commits)")[![NHZEX](https://avatars.githubusercontent.com/u/14545600?v=4)](https://github.com/NHZEX "NHZEX (8 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (4 commits)")[![shuipf](https://avatars.githubusercontent.com/u/2285144?v=4)](https://github.com/shuipf "shuipf (2 commits)")[![simplewindorg](https://avatars.githubusercontent.com/u/6101935?v=4)](https://github.com/simplewindorg "simplewindorg (2 commits)")[![bzp2010](https://avatars.githubusercontent.com/u/8078418?v=4)](https://github.com/bzp2010 "bzp2010 (1 commits)")[![qq475281441](https://avatars.githubusercontent.com/u/14217045?v=4)](https://github.com/qq475281441 "qq475281441 (1 commits)")[![OverNaive](https://avatars.githubusercontent.com/u/62283101?v=4)](https://github.com/OverNaive "OverNaive (1 commits)")[![hainuo](https://avatars.githubusercontent.com/u/421861?v=4)](https://github.com/hainuo "hainuo (1 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/topthink-think-swoole/health.svg)

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

###  Alternatives

[laravel/framework

The Laravel Framework.

34.7k509.9M17.0k](/packages/laravel-framework)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[drupal/core

Drupal is an open source content management platform powering millions of websites and applications.

19562.3M1.3k](/packages/drupal-core)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)

PHPackages © 2026

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