2026-05-05 14:44:26 +00:00
[](https://github.com/blax-software)
2026-04-02 10:44:16 +00:00
# Laravel WebSockets
2018-11-20 08:54:06 +00:00
2026-05-05 14:44:26 +00:00
[](https://php.net)
[](https://laravel.com)
Plug-and-play WebSockets for Laravel with a Pusher-compatible protocol, async fork-based handling, attribute-driven routing, and live operational tooling.
2026-04-02 10:44:16 +00:00
> [!NOTE]
> This package is actively maintained as a fork of beyondcode/laravel-websockets.
2026-05-05 14:44:26 +00:00
## 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_fork` child processes, so a slow handler never blocks the event loop
- **Broadcast from anywhere** — `ws_broadcast()` , `ws_whisper()` , `ws_broadcast_except()` helpers and a static `WebsocketService` API let any controller, job, or service push events to any channel
- **Multiple channel types** — public, `private-*` , `presence-*` , and `open-presence-*` channels with the standard auth handshake
- **Live ops tooling** — `php artisan websockets:watch` renders 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 |
+-----------+-------------+
```
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
- **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:clear` clears 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 legacy `pusher:*` action formats, drop-in for Echo and pusher-js clients
- **Test helpers** — `newConnection()` , `newActiveConnection()` , `newPrivateConnection()` , `newPresenceConnection()` , plus `assertSentEvent()` keep WebSocket tests short
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
## Requirements
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
- PHP 8.1+
- Laravel 9, 10, 11 or 12
- `ext-pcntl` (for async fork-based handling)
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
## Installation
2026-04-02 10:44:16 +00:00
```bash
composer require blax-software/laravel-websockets
```
2026-05-05 14:44:26 +00:00
Publish the config:
2026-04-02 10:44:16 +00:00
```bash
2026-05-05 14:44:26 +00:00
php artisan vendor:publish --provider="BlaxSoftware\LaravelWebSockets\WebSocketsServiceProvider" --tag="config"
2026-04-02 10:44:16 +00:00
```
2026-05-05 14:44:26 +00:00
Start the server:
2026-04-02 10:44:16 +00:00
```bash
php artisan websockets:serve
```
2026-05-05 14:44:26 +00:00
Default URL is `ws://127.0.0.1:6001` .
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
## Quick Start
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
### 1. Mark a regular controller method as WebSocket-reachable
2026-04-02 10:44:16 +00:00
```php
2026-05-05 14:44:26 +00:00
use BlaxSoftware\LaravelWebSockets\Attributes\Websocket;
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
class FlightschoolController extends Controller
{
#[Websocket] // event: "flightschool.index"
public function index() { ... }
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
#[Websocket(event: 'flightschools.list')] // explicit override
public function list() { ... }
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
#[Websocket(needAuth: true)] // requires authenticated socket
public function update() { ... }
2026-04-02 10:44:16 +00:00
}
```
2026-05-05 14:44:26 +00:00
### 2. Broadcast from anywhere
2026-04-02 10:44:16 +00:00
```php
2026-05-05 14:44:26 +00:00
// 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
2026-04-02 10:44:16 +00:00
use BlaxSoftware\LaravelWebSockets\Services\WebsocketService;
WebsocketService::send('metrics.tick', ['count' => 1], 'websocket');
WebsocketService::broadcastExcept('chat.message', ['text' => 'Hi'], ['1234.1'], 'chat');
2026-05-05 14:44:26 +00:00
```
### 3. Build a private/presence auth payload
```php
$auth = wsSession('private-updates', [
'user_id' => 7,
'user_info' => ['name' => 'Jane'],
]);
```
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
### 4. Watch live stats
```bash
php artisan websockets:watch # connection counts and channels
php artisan websockets:watch -v # expanded per-connection rows
php artisan websockets:info # one-shot snapshot
2026-04-02 10:44:16 +00:00
```
2026-05-05 14:44:26 +00:00
### 5. Iterate without restarting
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
```bash
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
```
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
## Channel Types
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
| 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 |
2026-04-02 10:44:16 +00:00
2026-05-05 14:44:26 +00:00
## Testing
2026-04-02 10:44:16 +00:00
```php
$connection = $this->newActiveConnection(['chat']);
$this->wsHandler->onMessage($connection, new Message([
2026-05-05 14:44:26 +00:00
'event' => 'websocket.ping',
'data' => new stdClass(),
2026-04-02 10:44:16 +00:00
]));
$connection->assertSentEvent('websocket.pong');
```
```bash
vendor/bin/phpunit --exclude-group=stability,stress,integration,requires-server
```
2018-11-20 08:54:06 +00:00
2018-12-04 23:48:30 +00:00
## Documentation
2018-12-04 21:36:12 +00:00
2026-04-02 10:44:16 +00:00
- Main docs: [docs ](docs )
- Getting started: [docs/getting-started/introduction.md ](docs/getting-started/introduction.md )
- Helper & testing guide: [docs/advanced-usage/helpers-and-testing.md ](docs/advanced-usage/helpers-and-testing.md )
2026-05-05 14:44:26 +00:00
- Custom handlers: [docs/advanced-usage/custom-websocket-handlers.md ](docs/advanced-usage/custom-websocket-handlers.md )
2025-01-16 07:54:02 +00:00
2026-04-02 10:44:16 +00:00
## Changelog
2018-11-20 08:54:06 +00:00
2026-04-02 10:44:16 +00:00
See [CHANGELOG ](CHANGELOG.md ).
2018-11-20 08:54:06 +00:00
2026-04-02 10:44:16 +00:00
## Security
2018-11-20 08:54:06 +00:00
2026-05-05 14:44:26 +00:00
Please report vulnerabilities via the issue tracker or by email: office@blax.at.
2018-11-20 08:54:06 +00:00
## Credits
2026-04-02 10:44:16 +00:00
- [Marcel Pociot ](https://github.com/mpociot )
2018-11-21 23:26:09 +00:00
- [Freek Van der Herten ](https://github.com/freekmurze )
2018-11-20 08:54:06 +00:00
- [All Contributors ](../../contributors )
## License
2026-04-02 10:44:16 +00:00
MIT. See [LICENSE.md ](LICENSE.md ).
2026-05-05 14:44:26 +00:00
## Star History
< a href = "https://www.star-history.com/?repos=blax-software%2Flaravel-websockets&type=date&legend=top-left" >
< picture >
< source media = "(prefers-color-scheme: dark)" srcset = "https://api.star-history.com/chart?repos=blax-software/laravel-websockets&type=date&theme=dark&legend=top-left" / >
< source media = "(prefers-color-scheme: light)" srcset = "https://api.star-history.com/chart?repos=blax-software/laravel-websockets&type=date&legend=top-left" / >
< img alt = "Star History Chart" src = "https://api.star-history.com/chart?repos=blax-software/laravel-websockets&type=date&legend=top-left" / >
< / picture >
< / a >