dimogrudev/php-websocket
Composer 安装命令:
composer require dimogrudev/php-websocket
包简介
No-dependency implementation of WebSocket server in PHP
关键字:
README 文档
README
A zero-dependency native implementation of WebSocket server in PHP.
Lightweight and minimalistic.
Features
- Strict adherence to RFC 6455
- Automatic HTTP Upgrade handshake compliant with RFC 9110 and RFC 9112
- SSL/TLS encryption support
- Binary and textual data message support, both sending and receiving
- Transparent reassembly of incoming fragmented messages
- Built-in handling for Ping, Pong, and Close control frames
- User-defined non-blocking timers
- Non-blocking I/O
Requirements
- PHP 8.4 or higher (64-bit)
Important
If you plan to run websockets on a shared hosting, note that most providers block ports for any third-party usage. A VPS or dedicated server is highly recommended.
Installation
This library may be installed via Composer:
composer require dimogrudev/php-websocket
Usage
use WebSocket\Server; use WebSocket\Contract\ClientInterface; use WebSocket\Contract\RequestInterface; use WebSocket\Contract\MessageInterface; require 'vendor/autoload.php'; // Create an instance of the server class // 0.0.0.0 is set as host to make the server reachable at all IPv4 addresses $server = new Server('0.0.0.0', 8443); // Enable encryption and provide certificate files $server->encryption(true, 'path/to/cert.crt', 'path/to/cert.key'); // Handle incoming messages and respond to clients $server->onMessageReceive(function (ClientInterface $client, MessageInterface $message): void { if ($message->isBinary) { echo "{$client->ipAddr} (#{$client->id}) sends binary message ({$message->length} bytes)\n"; // Echo the binary message back to the client $client->send($message->payload, isBinary: true); } else { echo "{$client->ipAddr} (#{$client->id}) sends `{$message->payload}`\n"; // Reply with a text message $client->send("Server received your message: {$message->payload}"); } }); $server->start();
Tip
Modern browsers block non-secure WebSocket connections (ws://) on secure websites (https://). Always use a valid SSL/TLS certificate (e.g. Let's Encrypt) for production.
Timers
// Create a timer to run function repeatedly with a 500 milliseconds interval // It provides timer ID which may be used for later cancellation $timerId = $server->setTimer(function (): void { // Your logic here }, 500, true); // Cancel the timer $server->clearTimer($timerId);
Callbacks Reference
| Method | Signature | Description |
|---|---|---|
onServerStart |
(): void |
Triggered when the server starts listening. |
onServerStop |
(): void |
Triggered when the server stops. |
onHandshake |
(ClientInterface, RequestInterface): bool |
Triggered when a handshake request is received. Return true to accept the connection. |
onClientConnect |
(ClientInterface): void |
Triggered after the handshake is accepted and the connection is fully established. |
onClientDisconnect |
(ClientInterface): void |
Triggered when the connection is closed. |
onMessageReceive |
(ClientInterface, MessageInterface): void |
Triggered when a complete data message is received. |
Connection Security
Unauthorized connections may be rejected by using the onHandshake callback:
$server->onHandshake(function (ClientInterface $client, RequestInterface $request): bool { // Reject connection if it's not from your trusted domain if ($request->header('origin') !== 'https://example.com') { return false; } return true; });
License
The MIT License (MIT). For more information, please see LICENSE.
dimogrudev/php-websocket 适用场景与选型建议
dimogrudev/php-websocket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「websocket」 「server」 「rfc6455」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dimogrudev/php-websocket 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dimogrudev/php-websocket 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dimogrudev/php-websocket 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Websockets for PHP using Rx
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A jQuery augmented PHP library for creating and validating HTML forms
annotations-scan-plugin
WebSocket view engine
rfc6455
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-19