PHPackages                             tystudy/yii2-swoole-websocket - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. tystudy/yii2-swoole-websocket

ActiveYii2-extension[HTTP &amp; Networking](/categories/http)

tystudy/yii2-swoole-websocket
=============================

利用swoole搭建了websocket服务器 不修改yii2直接使用

11172PHP

Since May 3Pushed 8y ago1 watchersCompare

[ Source](https://github.com/tystudy/yii2-swoole-websocket)[ Packagist](https://packagist.org/packages/tystudy/yii2-swoole-websocket)[ RSS](/packages/tystudy-yii2-swoole-websocket/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

yii2-swoole-websocket
---------------------

[](#yii2-swoole-websocket)

利用swoole搭建了websocket服务器 不修改yii2直接使用 可以根据demo进行修改满足自己的websocket使用场景 测试使用 php7.2 swoole1.10.3 yii2-advanced（目前只支持advanced）

安装
--

[](#安装)

1.使用composer composer的安装以及国内镜像设置请点击[此处](http://www.phpcomposer.com/)如果无法安装 请把yii2的composer.json 修改 "minimum-stability": "dev",

```
$ composer require tystudy/yii2-swoole-websocket -vvv
```

or add

```
"tystudy/yii2-swoole-websocket": "*"
```

---

2.配置yii2

打开console/config/main.php（注意：并不是配置在components里面，而应该在最外层，即与components同级）。

```
'id' => 'app-console',
 ...//其他配置
'controllerMap'=>[
     ...//其他配置项
   'websocket' => [
		'class' => 'tystudy\swoole\console\SwooleController',
		'rootDir' => str_replace('console/config', '', __DIR__ ),//yii2项目根路径
		'app' => 'backend',
		'host' => '0.0.0.0',                                    //默认监听所有机器  可以填写127.0.0.1只监听本机 详见swoole文档
		'port' => 9998,
		'web' => 'web',                                         //默认为web rootDir app web目的是拼接yii2的根目录
		'debug' => true,                                        //默认开启debug，上线应置为false
		'env' => 'dev',                                         //默认为dev，上线应置为prod
		'swooleConfig' => [                                     //swoole 相关配置 详见swoole文档
			'reactor_num' => 2,
			'worker_num' => 4,
			'task_worker_num' => 4,
			'daemonize' => false,
			'log_file' => __DIR__ . '/../../backend/runtime/logs/swoole.log',
			'log_level' => 0,
			'pid_file' => __DIR__ . '/../../backend/runtime/server.pid',
		],
		'project'=>[                                           //项目server路径 根据实际情况填写
			'server'=>[
				'tystudy\swoole\project\ProjectWebsocketServer',
			],
		]
	],
    ...//其他配置
]
 ...//其他配置
```

---

3.启动命令 linux 进入项目目录

```
* 启动 ./yii websocket/start
* 关闭 ./yii websocket/stop
* 重启 ./yii websocket/restart

```

---

4.project项目demo文件 实现2人聊天的websocket应用

```
使用说明：
* 1.yii2 控制台中启动server  linux 下使用命令 ./yii websocket/start
* 2.将project文件夹下的chat_server_websocket.php chat_server_websocket2.php 放任意可访问目录，修改websocket server IP
* 3.聊天使用mysql存储
```

---

5.项目扩展应用 参考project使用方法

---

6.说明 由于swoole的websocket server具有http server的功能，可以利用websocket server来实现对http请求的响应 一般不建议这样处理，线上环境通常是apache或nginx比swoole的功能更加完善，建议用来单纯的做websocket服务器 实现php的长链接和一些推送任务，如果需要，可以使用多个swoole服务器，用nginx实现负载均衡

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/861554f51e05ca94e2e823a3941e7e01153e47427ed4aa91c6178238a88ba846?d=identicon)[tystudy](/maintainers/tystudy)

### Embed Badge

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

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

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M318](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[nyholm/psr7

A fast PHP7 implementation of PSR-7

1.3k235.4M2.4k](/packages/nyholm-psr7)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M292](/packages/pusher-pusher-php-server)[spatie/crawler

Crawl all internal links found on a website

2.8k16.3M52](/packages/spatie-crawler)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78126.4M414](/packages/react-http)

PHPackages © 2026

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