ez-php/websocket
Composer 安装命令:
composer require ez-php/websocket
包简介
PHP 8.5 Fiber-based WebSocket server for ez-php — RFC 6455 handshake, frame codec, ChannelManager pub/sub, and a non-blocking event loop.
README 文档
README
PHP 8.5 Fiber-based WebSocket server for the ez-php ecosystem.
Implements RFC 6455 from the ground up — no third-party WebSocket library needed. Each connection runs in its own Fiber, allowing hundreds of concurrent clients on a single PHP process without threads or async extensions.
Intentionally separate from ez-php/broadcast (SSE/event-bus); this module
provides bidirectional, low-latency real-time communication.
Installation
composer require ez-php/websocket
No framework integration is needed — the server runs as a standalone long-lived process.
Quick start
<?php use EzPhp\WebSocket\ChannelManager; use EzPhp\WebSocket\ConnectionInterface; use EzPhp\WebSocket\Frame; use EzPhp\WebSocket\HandlerInterface; use EzPhp\WebSocket\Server; class ChatHandler implements HandlerInterface { public function __construct(private readonly ChannelManager $channels) {} public function onOpen(ConnectionInterface $conn): void { $this->channels->subscribe('general', $conn); $this->channels->broadcast('general', "{$conn->id()} joined."); } public function onMessage(ConnectionInterface $conn, Frame $frame): void { $this->channels->broadcast('general', "[{$conn->id()}] {$frame->payload}"); } public function onClose(ConnectionInterface $conn): void { $this->channels->unsubscribeAll($conn); $this->channels->broadcast('general', "{$conn->id()} left."); } public function onError(ConnectionInterface $conn, \Throwable $e): void { error_log("WebSocket error [{$conn->id()}]: {$e->getMessage()}"); } } $server = new Server('0.0.0.0', 8080); $server->run(new ChatHandler(new ChannelManager()));
Start the server:
php chat-server.php
Connect from a browser:
const ws = new WebSocket('ws://localhost:8080'); ws.onmessage = e => console.log(e.data); ws.onopen = () => ws.send('Hello!');
Core classes
Server
$server = new Server(host: '0.0.0.0', port: 8080); $server->run($handler); // blocks; handles SIGTERM externally
- Opens a TCP server socket via
stream_socket_server() - Spawns one
Fiberper accepted connection - Non-blocking
stream_select()event loop resumes Fibers when sockets are readable - Handles PING→PONG and CLOSE frames automatically
ConnectionInterface / Connection
$conn->id(); // unique string ID assigned by Server $conn->isConnected(); // false after close() or peer disconnect $conn->send('text'); // UTF-8 TEXT frame $conn->sendBinary($b); // BINARY frame $conn->close('bye'); // clean close with status 1000
Frame
Represents one RFC 6455 frame. Available in HandlerInterface::onMessage():
$frame->opcode; // Opcode::TEXT | Opcode::BINARY $frame->payload; // decoded (unmasked) payload string $frame->fin; // true for complete (non-fragmented) messages
ChannelManager
Named pub/sub channels over connected clients:
$mgr->subscribe('room', $conn); $mgr->unsubscribe('room', $conn); $mgr->unsubscribeAll($conn); // called in onClose() $mgr->broadcast('room', 'message'); // sends TEXT frame to all connected subscribers $mgr->connections('room'); // list<ConnectionInterface> $mgr->channels(); // list<string> $mgr->count('room'); // int
broadcast() silently prunes disconnected connections.
Architecture notes
- No message fragmentation reassembly: continuation frames are silently ignored. Real-world clients send single-frame messages for chat/notifications; large binary transfers should be chunked at the application level.
- No TLS (WSS): terminate TLS at a reverse proxy (nginx, Caddy) and use plain
ws://internally. - No authentication: verify cookies or tokens in
onOpen()and call$conn->close()on failure.
License
MIT
ez-php/websocket 适用场景与选型建议
ez-php/websocket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「websocket」 「realtime」 「rfc6455」 「fiber」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ez-php/websocket 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ez-php/websocket 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ez-php/websocket 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
WebSocket view engine
Alfabank REST API integration
This is a PHP Framework base on Swoole
PHP Binance API is an asynchronous PHP library for the Binance API designed to be easy to use.
High-performance, opinionated PHP 8 web framework with O(1) routing, parallel async MySQL, type-safe asset bridge, and React-island frontend
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 43
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-28