blax-software/laravel-websockets
Composer 安装命令:
composer require blax-software/laravel-websockets
包简介
An easy to launch a Pusher-compatible WebSockets server for Laravel.
README 文档
README
Laravel WebSockets
Plug-and-play WebSockets for Laravel with a Pusher-compatible protocol, async fork-based handling, attribute-driven routing, and live operational tooling.
Note
This package is actively maintained as a fork of beyondcode/laravel-websockets.
Features
-
#[Websocket]attribute on regular HTTP controllers — turn any controller method into a WebSocket-callable endpoint with one annotation, no second class to maintain -
Async processing — incoming messages are handled in
pcntl_forkchild processes, so a slow handler never blocks the event loop -
Broadcast from anywhere —
ws_broadcast(),ws_whisper(),ws_broadcast_except()helpers and a staticWebsocketServiceAPI let any controller, job, or service push events to any channel -
Multiple channel types — public,
private-*,presence-*, andopen-presence-*channels with the standard auth handshake -
Live ops tooling —
php artisan websockets:watchrenders connection counts, authenticated users, and per-channel connections, refreshing every second:WebSocket Server — Live Stats 2026-05-05 14:33:35 — refreshing every 1s (Ctrl+C to exit) Live Stats ....................................................................................................................................... Total connections ............................................................................................................................. 12 Authenticated users ............................................................................................................................ 3 Active channels ................................................................................................................................ 1 +-----------+-------------+ | Channel | Connections | +-----------+-------------+ | websocket | 12 | +-----------+-------------+ -
Automatic route recognition — controller class and method names map to event names automatically (
FlightschoolController::index→flightschool.index); override per method or per class via#[Websocket(event: ..., prefix: ..., suffix: ..., needAuth: true)] -
Hot code reload in dev, OPcache in prod —
websocket:steer cache:clearclears OPcache and the controller resolver cache without restarting the running server, so iteration is instant in development while production runs with fully warmed caches -
Pusher-compatible protocol — supports both modern
websocket.*and legacypusher:*action formats, drop-in for Echo and pusher-js clients -
Test helpers —
newConnection(),newActiveConnection(),newPrivateConnection(),newPresenceConnection(), plusassertSentEvent()keep WebSocket tests short
Requirements
- PHP 8.1+
- Laravel 9, 10, 11 or 12
ext-pcntl(for async fork-based handling)
Installation
composer require blax-software/laravel-websockets
Publish the config:
php artisan vendor:publish --provider="BlaxSoftware\LaravelWebSockets\WebSocketsServiceProvider" --tag="config"
Start the server:
php artisan websockets:serve
Default URL is ws://127.0.0.1:6001.
Quick Start
1. Mark a regular controller method as WebSocket-reachable
use BlaxSoftware\LaravelWebSockets\Attributes\Websocket; class FlightschoolController extends Controller { #[Websocket] // event: "flightschool.index" public function index() { ... } #[Websocket(event: 'flightschools.list')] // explicit override public function list() { ... } #[Websocket(needAuth: true)] // requires authenticated socket public function update() { ... } }
2. Broadcast from anywhere
// Helpers ws_broadcast('chat.message', ['text' => 'Hello'], 'chat'); ws_whisper('chat.typing', ['typing' => true], ['1234.1', '1234.2'], 'chat'); ws_broadcast_except('chat.message', ['text' => 'Server msg'], ['1234.1'], 'chat'); // Service API use BlaxSoftware\LaravelWebSockets\Services\WebsocketService; WebsocketService::send('metrics.tick', ['count' => 1], 'websocket'); WebsocketService::broadcastExcept('chat.message', ['text' => 'Hi'], ['1234.1'], 'chat');
3. Per-connection session store
Inside a WebSocket handler, wsSession() returns a Redis-backed key/value store scoped to the
current connection that persists across messages on the same socket. It takes no arguments.
wsSession()->increment('transmit_count'); $count = wsSession()->get('transmit_count', 0); wsSession()->put('last_action', 'transmitted');
4. Watch live stats
php artisan websockets:watch # connection counts and channels php artisan websockets:watch -v # expanded per-connection rows php artisan websockets:info # one-shot snapshot
5. Iterate without restarting
php artisan websocket:steer cache:clear # clear OPcache + resolver cache php artisan websockets:restart # graceful restart php artisan websocket:restart-hard # signal-based force restart
Channel Types
| Type | Prefix | Description |
|---|---|---|
| Public | (none) | Anyone can subscribe |
| Private | private- |
Server-signed auth required |
| Presence | presence- |
Auth required, tracks user list, broadcasts join/leave |
| Open Presence | open-presence- |
Presence semantics without the auth signature — useful for guests |
Testing
$connection = $this->newActiveConnection(['chat']); $this->wsHandler->onMessage($connection, new Message([ 'event' => 'websocket.ping', 'data' => new stdClass(), ])); $connection->assertSentEvent('websocket.pong');
vendor/bin/phpunit --exclude-group=stability,stress,integration,requires-server
Documentation
- Main docs: docs
- Getting started: docs/getting-started/introduction.md
- Helper & testing guide: docs/advanced-usage/helpers-and-testing.md
- Custom handlers: docs/advanced-usage/custom-websocket-handlers.md
Changelog
See CHANGELOG.
Security
Please report vulnerabilities via the issue tracker or by email: office@blax.at.
Credits
License
MIT. See LICENSE.md.
Star History
blax-software/laravel-websockets 适用场景与选型建议
blax-software/laravel-websockets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 228 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「beyondcode」 「laravel-websockets」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 blax-software/laravel-websockets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 blax-software/laravel-websockets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 blax-software/laravel-websockets 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Add Server-Timing header information from within your Laravel apps.
Perform various self diagnosis tests on your Laravel application.
Marcel Pociot's based beyondcode/laravel-visual-diff adapted for Laravel 7
A PHP SDK for building Claude Code hooks.
Alfabank REST API integration
Add comments to your Laravel application
统计信息
- 总下载量: 228
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-31