PHPackages                             topthink/think-worker - 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. topthink/think-worker

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

topthink/think-worker
=====================

workerman extend for thinkphp

v5.0.2(1y ago)202227.2k↓12.9%60[3 issues](https://github.com/top-think/think-worker/issues)9Apache-2.0PHPPHP &gt;=8.2

Since Oct 4Pushed 9mo ago11 watchersCompare

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

READMEChangelog (10)Dependencies (10)Versions (33)Used By (9)

ThinkPHP Workerman 扩展
=====================

[](#thinkphp-workerman-扩展)

交流群：981069000 [![点击加群](https://camo.githubusercontent.com/75b53e353bb9e5064662e185a6d39f4bb88c4e45bd3a1240ddf599525edb6afa/68747470733a2f2f7075622e69647171696d672e636f6d2f7770612f696d616765732f67726f75702e706e67 "点击加群")](https://qm.qq.com/q/A8YNpzrzC8)

安装
--

[](#安装)

```
composer require topthink/think-worker

```

说明
--

[](#说明)

> 由于windows下无法在一个文件里启动多个worker，所以本扩展不支持windows平台

使用方法
----

[](#使用方法)

### HttpServer

[](#httpserver)

在命令行启动服务端

```
php think worker

```

然后就可以通过浏览器直接访问当前应用

```
http://localhost:8080

```

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

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

[](#访问静态文件)

> 建议使用nginx来支持静态文件访问，也可使用路由输出文件内容，下面是示例，可参照修改

1. 添加静态文件路由：

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

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

队列支持
----

[](#队列支持)

使用方法见 [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服务响应不同的事件

#### 配置

[](#配置)

```
worker.websocket = true 时开启

```

#### 路由定义

[](#路由定义)

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

#### 控制器

[](#控制器)

```
use \think\worker\Websocket;
use \think\worker\websocket\Frame;

class Controller {

    public function action1(){

        return (new \think\worker\response\Websocket())
            ->onOpen(...)
            ->onMessage(function(Websocket $websocket, Frame $frame){
                ...
            })
            ->onClose(...);
    }

    public function action2(){

        return (new \think\worker\response\Websocket())
            ->onOpen(...)
            ->onMessage(function(Websocket $websocket, Frame $frame){
               ...
            })
            ->onClose(...);
    }
}
```

自定义worker
---------

[](#自定义worker)

监听`worker.init`事件 注入`Manager`对象，调用addWorker方法添加

```
use think\worker\Manager;
use \think\worker\Worker;

//...

public function handle(Manager $manager){
   $worker = $manager->addWorker(function(Worker $worker){
        //..其他回调或处理
        //动态添加监听可参考 https://www.workerman.net/doc/workerman/worker/listen.html
    });
}

//...
```

###  Health Score

59

—

FairBetter than 99% of packages

Maintenance50

Moderate activity, may be stable

Popularity53

Moderate usage in the ecosystem

Community28

Small or concentrated contributor base

Maturity87

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 81.5% 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 ~103 days

Recently: every ~53 days

Total

32

Last Release

294d ago

Major Versions

v1.0.1 → v2.0.02018-06-25

v2.0.12 → v3.0.02019-04-23

2.0.x-dev → v3.0.52020-04-30

v3.0.7 → v4.0.02023-07-20

4.0.x-dev → v5.0.0-rc.02024-12-28

### Community

Maintainers

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

---

Top Contributors

[![liu21st](https://avatars.githubusercontent.com/u/1111670?v=4)](https://github.com/liu21st "liu21st (101 commits)")[![yunwuxin](https://avatars.githubusercontent.com/u/2168125?v=4)](https://github.com/yunwuxin "yunwuxin (20 commits)")[![NHZEX](https://avatars.githubusercontent.com/u/14545600?v=4)](https://github.com/NHZEX "NHZEX (3 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[symplify/monorepo-builder

Not only Composer tools to build a Monorepo.

5205.3M82](/packages/symplify-monorepo-builder)[coenjacobs/mozart

Composes all dependencies as a package inside a WordPress plugin

4723.6M20](/packages/coenjacobs-mozart)[illuminate/session

The Illuminate Session package.

9937.4M753](/packages/illuminate-session)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[laragear/preload

Effortlessly make a Preload script for your Laravel application.

119363.5k](/packages/laragear-preload)[friendsoftypo3/content-blocks

TYPO3 CMS Content Blocks - Content Types API | Define reusable components via YAML

96374.6k23](/packages/friendsoftypo3-content-blocks)

PHPackages © 2026

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