PHPackages                             mix/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. mix/websocket

ActiveLibrary[HTTP &amp; Networking](/categories/http)

mix/websocket
=============

WebSocket server and client based on Swoole coroutine

v3.0.12(4y ago)26.6k4Apache-2.0PHPPHP &gt;=7.0.0CI failing

Since Dec 27Pushed 4y ago1 watchersCompare

[ Source](https://github.com/mix-php/websocket)[ Packagist](https://packagist.org/packages/mix/websocket)[ Docs](https://openmix.org/mix-php)[ RSS](/packages/mix-websocket/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (36)Used By (4)

> OpenMix 出品：[https://openmix.org](https://openmix.org/mix-php)

Mix WebSocket
=============

[](#mix-websocket)

PHP WebSocket server and client based on Swoole coroutine

基于 Swoole 协程的 PHP WebSocket 服务器与客户端

Overview
--------

[](#overview)

该 WebSocket 支持处理服务器和客户端，服务器基于 Swoole 单进程协程 `Swoole\Coroutine\Http\Server` 驱动，没有多进程那些复杂的作用域和生命周期概念，开发体验和 Golang 一致，简单又高效。

**推荐搭配以下数据库使用 (支持协程和连接池)：**

-
-

**推荐搭配以下库处理 Subscribe：**

-

技术交流
----

[](#技术交流)

知乎：
官方QQ群：[284806582](https://shang.qq.com/wpa/qunwpa?idkey=b3a8618d3977cda4fed2363a666b081a31d89e3d31ab164497f53b72cf49968a), [825122875](http://shang.qq.com/wpa/qunwpa?idkey=d2908b0c7095fc7ec63a2391fa4b39a8c5cb16952f6cfc3f2ce4c9726edeaf20)敲门暗号：ws

Installation
------------

[](#installation)

- Swoole &gt;= 4.4.15:

```
composer require mix/websocket

```

服务器 Server
----------

[](#服务器-server)

在 [Mix Vega](https://github.com/mix-php/vega) 中使用 (只支持 Swoole 单进程协程)

```
$upgrader = new Mix\WebSocket\Upgrader();

$vega = new Mix\Vega\Engine();
$vega->handle('/websocket', function (Mix\Vega\Context $ctx) use ($upgrader) {
    // 升级连接
    $conn      = $upgrader->upgrade($ctx->request, $ctx->response);

    // 接收消息
    $in        = $conn->readMessage();
    var_dump($in->data);

    // 发送消息
    $out       = new Swoole\WebSocket\Frame();
    $out->data = sprintf('hello, %s', $in->data);
    $conn->send($out);

    $conn->close();
})->methods('GET');
```

在 Swoole 原生中使用 (只支持单进程协程)

```
Swoole\Coroutine\run(function () {
    $upgrader = new Mix\WebSocket\Upgrader();
    $server = new Swoole\Coroutine\Http\Server('0.0.0.0', 9502, false);
    $server->handle('/websocket', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($upgrader) {
        // 升级连接
        $conn = $upgrader->upgradeRaw($request, $response);

        // ...
    });
    $server->start();
});
```

获取当前连接数

```
$total = $upgrader->count();
```

关闭全部连接

```
$upgrader->closeAll();
```

客户端 Client
----------

[](#客户端-client)

可以连接任何 websocket v13 的服务器

```
$cli   = Mix\WebSocket\Client('ws://127.0.0.1:9502/websocket');

// 发送消息
$out       = new Swoole\WebSocket\Frame();
$out->data = 'xiaoming';
$cli->writeMessage($out);

// 接收消息
$in = $cli->readMessage();
var_dump($in->data);

$cli->close();
```

License
-------

[](#license)

Apache License Version 2.0,

###  Health Score

33

—

LowBetter than 74% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity67

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 ~32 days

Recently: every ~25 days

Total

35

Last Release

1604d ago

Major Versions

v2.2.18 → v3.0.12021-07-14

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16074765?v=4)[LIU JIAN](/maintainers/onanying)[@onanying](https://github.com/onanying)

---

Top Contributors

[![onanying](https://avatars.githubusercontent.com/u/16074765?v=4)](https://github.com/onanying "onanying (60 commits)")

---

Tags

coroutinemixswoolewebsocketclientserverwebsocketswoolecoroutinemix

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mix-websocket/health.svg)

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

###  Alternatives

[hhxsv5/laravel-s

🚀 LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.

3.9k676.0k10](/packages/hhxsv5-laravel-s)[swow/swow

Coroutine-based multi-platform support engine with a focus on concurrent I/O

1.3k2.1M84](/packages/swow-swow)[phrity/websocket

WebSocket client and server

2184.1M27](/packages/phrity-websocket)[swlib/saber

Swoole coroutine HTTP client

985145.0k27](/packages/swlib-saber)[swoole/grpc

Grpc PHP Client base on Swoole Http2 Coroutine

19718.9k1](/packages/swoole-grpc)[swoole/etcd-client

Grpc PHP Client base on Swoole Http2 Coroutine

1973.2k1](/packages/swoole-etcd-client)

PHPackages © 2026

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