artisan-build/pusher-websocket-php
Composer 安装命令:
composer require artisan-build/pusher-websocket-php
包简介
Pusher Channels websocket client library for PHP
关键字:
README 文档
README
A PHP WebSocket client for Pusher Channels and Pusher-compatible servers. This is a port of pusher-js implementing Pusher Protocol v7.
When to Use This Package
This package is a low-level WebSocket client for connecting to Pusher-compatible servers from PHP. It's designed for:
- CLI applications that need real-time communication
- Background workers or daemons
- Non-HTTP PHP processes that need WebSocket connections
- Building higher-level abstractions
Laravel users: You probably want artisan-build/resonance instead, which provides Laravel-specific integrations and brings this package as a dependency.
Requirements
- PHP 8.3 or higher
- A Pusher-compatible WebSocket server:
Installation
composer require artisan-build/pusher-websocket-php
Usage
use ArtisanBuild\Pusher\Pusher; use ArtisanBuild\Pusher\Options; $pusher = new Pusher('your-app-key', new Options( cluster: 'mt1', wsHost: '127.0.0.1', wsPort: 6001, forceTLS: false, )); // Subscribe to a channel $channel = $pusher->subscribe('my-channel'); // Listen for events $channel->bind('my-event', function ($data) { echo "Received: " . json_encode($data) . "\n"; }); // Connect to the server $pusher->connect();
Subscribing to Channels
// Public channel $channel = $pusher->subscribe('news'); // Private channel (requires authentication) $privateChannel = $pusher->subscribe('private-user-123'); // Presence channel (requires authentication) $presenceChannel = $pusher->subscribe('presence-chat-room');
Binding to Events
// Bind to a specific event $channel->bind('message', function ($data) { // Handle the event }); // Bind to all events on a channel $channel->bind_global(function ($event, $data) { echo "Event: {$event}\n"; }); // Unbind $channel->unbind('message');
Configuration Options
new Options( cluster: 'mt1', // Pusher cluster (required for Pusher, empty for self-hosted) wsHost: '127.0.0.1', // WebSocket host wsPort: 6001, // WebSocket port (non-TLS) wssPort: 443, // WebSocket port (TLS) forceTLS: true, // Force TLS connection activityTimeout: 120000, // Milliseconds before sending ping pongTimeout: 30000, // Milliseconds to wait for pong channelAuthorization: [ // For private/presence channels 'transport' => 'http', 'endpoint' => 'https://your-app.com/broadcasting/auth', 'headers' => [ 'Authorization' => 'Bearer your-token', ], ], );
Channel Authorization
Private and presence channels require authorization. The http transport makes a POST request to your auth endpoint:
$pusher = new Pusher('app-key', new Options( wsHost: '127.0.0.1', wsPort: 8080, channelAuthorization: [ 'transport' => 'http', 'endpoint' => 'https://your-app.com/broadcasting/auth', 'headers' => [ 'Authorization' => 'Bearer ' . $token, 'Accept' => 'application/json', ], ], )); // Now private channels will authenticate automatically $channel = $pusher->subscribe('private-user-123');
For custom authorization logic, use a customHandler:
channelAuthorization: [
'customHandler' => function ($params, $callback) {
// $params->socketId - the socket ID
// $params->channelName - the channel being authorized
// Make your own auth request...
$authData = myCustomAuth($params);
// Call back with result
$callback(null, new ChannelAuthorizationData(auth: $authData['auth']));
},
],
Development
Prerequisites
You'll need a Pusher-compatible WebSocket server running locally for integration tests. Options include:
Soketi (recommended for development):
# Via npm (requires Node.js 14-18) npx soketi start # Via Docker docker run -p 6001:6001 quay.io/soketi/soketi:1.6-16-debian
Laravel Reverb (if using Laravel Herd):
php artisan reverb:start
Environment Setup
Copy the example environment file and configure your server:
cp .env.example .env
Edit .env with your server details:
PUSHER_HOST=127.0.0.1 PUSHER_PORT=6001 PUSHER_SCHEME=ws PUSHER_APP_ID=app-id PUSHER_APP_KEY=app-key PUSHER_APP_SECRET=app-secret PUSHER_CLUSTER=mt1
Running Tests
# Unit and feature tests (no server required) composer test # Integration tests (requires running server) composer test:integration # All tests composer test:all # With coverage composer test:coverage # Type coverage composer test:types
Code Quality
# Linting composer lint # Static analysis composer stan
Architecture
This package is built on ReactPHP for asynchronous I/O:
ratchet/pawl- WebSocket clientreact/event-loop- Event loopreact/promise- Promises/async
The codebase follows the structure of pusher-js, ported to idiomatic PHP with full type coverage.
Contributing
Contributions are welcome! Please ensure:
- Tests pass:
composer test - Code style is correct:
composer lint - Static analysis passes:
composer stan - Type coverage remains at 100%:
composer test:types
Security Vulnerabilities
Please report security vulnerabilities to security@artisan.build.
Credits
This package is a PHP port of pusher-js by Pusher.
License
The MIT License (MIT). Please see License File for more information.
artisan-build/pusher-websocket-php 适用场景与选型建议
artisan-build/pusher-websocket-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 320 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「websocket」 「pusher」 「realtime」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 artisan-build/pusher-websocket-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 artisan-build/pusher-websocket-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 artisan-build/pusher-websocket-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Talk is a Laravel based realtime messaging, chatting and conversation system. It helps to develop users messaging, chatting and conversations in easy way.
Standalone PHP library for easy devices notifications push.
Module handling the real time notifications
Pusher Beams is a push notification service from Pusher.
Send push notification to android|ios devices , support APNs, FCM
A Pusher bridge for Laravel 5+
统计信息
- 总下载量: 320
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 33
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-13