sockudo / laravel
Official Laravel broadcasting driver and service-container integration for Sockudo
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.15.3
- illuminate/broadcasting: ^12.0|^13.0
- illuminate/console: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- sockudo/sockudo-php-server: ^2.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.80
- orchestra/testbench: ^10.0|^11.0
- overtrue/phplint: ^9.7
- phpunit/phpunit: ^11.0|^12.0|^13.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Official Laravel broadcasting driver and service-container integration for Sockudo.
The package keeps normal Laravel events on Sockudo's Pusher-compatible Protocol V1 surface while exposing native history, mutable messages, annotations, and push APIs through the Sockudo facade.
Requirements
- PHP 8.2 or newer
- Laravel 12 or 13
- A reachable Sockudo server and configured application credentials
Install
composer require sockudo/laravel php artisan sockudo:install
Configure the application without committing secrets:
BROADCAST_CONNECTION=sockudo SOCKUDO_APP_ID=app-id SOCKUDO_APP_KEY=app-key SOCKUDO_APP_SECRET=app-secret SOCKUDO_HOST=127.0.0.1 SOCKUDO_PORT=6001 SOCKUDO_SCHEME=http
Use HTTPS whenever the HTTP API crosses an untrusted network. Validate the configuration and signed HTTP API connection with:
php artisan sockudo:check
The package auto-discovers its service provider and adds a sockudo
broadcasting connection unless the application already defines one with that
name. Publish config/sockudo.php when you need to customize the connection.
If routes/channels.php is absent, install Laravel's broadcasting routes before
using private or presence channels; sockudo:install reports this condition.
Broadcast Laravel events
Existing Laravel events work without Sockudo-specific interfaces:
use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; final class OrderUpdated implements ShouldBroadcast { public function __construct(public readonly string $orderId) { } public function broadcastOn(): PrivateChannel { return new PrivateChannel("orders.{$this->orderId}"); } }
ShouldBroadcastNow, queued broadcasts, broadcast(...)->toOthers(), private
channels, presence channels, user authentication, and encrypted private
channels use Laravel's normal broadcasting behavior.
Define private and presence authorization in routes/channels.php as usual:
use Illuminate\Support\Facades\Broadcast; Broadcast::channel('orders.{order}', function ($user, $order) { return $user->can('view', $order); });
Configure Laravel Echo
Use the Protocol V2 connector from @sockudo/client:
npm install laravel-echo @sockudo/client
import Echo from 'laravel-echo'; import { SockudoConnector } from '@sockudo/client/echo'; window.Echo = new Echo({ broadcaster: SockudoConnector, key: import.meta.env.VITE_SOCKUDO_APP_KEY, wsHost: import.meta.env.VITE_SOCKUDO_HOST, wsPort: Number(import.meta.env.VITE_SOCKUDO_PORT ?? 6001), wssPort: Number(import.meta.env.VITE_SOCKUDO_PORT ?? 443), forceTLS: (import.meta.env.VITE_SOCKUDO_SCHEME ?? 'https') === 'https', connectionRecovery: true, });
Private and presence subscriptions still authorize through Laravel's
/broadcasting/auth route. Do not expose the app secret to JavaScript. Echo's
Pusher connector with pusher-js keeps working for Protocol V1.
Protocol V2 publish metadata
use Sockudo\Laravel\Broadcasting\InteractsWithSockudo; final class OrderUpdated implements ShouldBroadcast { use InteractsWithSockets, InteractsWithSockudo; public function __construct(public string $orderId, int $version) { $this->withSockudoTags(['region' => 'eu']) ->withSockudoIdempotencyKey("order.updated:{$orderId}:v{$version}"); } }
Events that define broadcastWith() return $this->sockudoPayload([...]).
Capability tokens
Sockudo::capabilitiesUsing(fn ($user, $request) => [ "private-App.Models.User.{$user->id}" => ['subscribe'], ]); Route::post('/sockudo/token', \Sockudo\Laravel\Http\CapabilityTokenController::class) ->middleware('auth');
Point Echo's authUrl option at the route.
Use native Sockudo APIs
The facade proxies to the default sockudo connection:
use Sockudo\Laravel\Facades\Sockudo; $page = Sockudo::getChannelHistory('orders', ['limit' => 50]); Sockudo::updateMessage('orders', $messageSerial, [ 'data' => ['status' => 'paid'], ]); Sockudo::publishAnnotation('orders', $messageSerial, [ 'type' => 'reaction', 'name' => 'confirmed', ]); $publish = Sockudo::publishPush([ 'recipients' => [['type' => 'channel', 'channel' => 'orders']], 'payload' => ['title' => 'Order updated'], 'publishId' => 'order-updated:ord-123:v4', ]); // Close every connection subscribed to a channel, on every node (close code 4009). $closed = Sockudo::terminateChannelConnections('private-orders')->closed_connections; // The app's current connection count across the cluster. $connections = Sockudo::getConnectionCount()->connections;
terminateChannelConnections and getConnectionCount require a sockudo/sockudo-php-server
release that includes them.
Apple Live Activity broadcast channels are available through
createApnsLiveActivityChannel, getApnsLiveActivityChannel,
listApnsLiveActivityChannels, and deleteApnsLiveActivityChannel. Publish direct or broadcast
ActivityKit updates with publishPush; see the monorepo's
Apple Live Activities guide.
Dependency injection is also available:
use Sockudo\SockudoInterface; final class LoadOrderHistory { public function __construct(private SockudoInterface $sockudo) { } }
For multiple apps, define additional driver => sockudo connections under
broadcasting.connections, then select one explicitly:
$client = Sockudo::connection('sockudo-eu');
Testing
composer install vendor/bin/php-cs-fixer fix --dry-run --diff --using-cache=no vendor/bin/phplint src config tests vendor/bin/phpunit
Development happens in the Sockudo monorepo under
server-sdks/sockudo-laravel. Report issues in the main Sockudo repository.