Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Broadcasting Laravel Package

illuminate/broadcasting

Illuminate Broadcasting provides Laravel’s broadcasting layer for sending real-time events over channels via drivers like Pusher, Ably, and Redis. It includes broadcaster contracts, channel authorization, and event broadcasting utilities for scalable pub/sub.

View on GitHub
Deep Wiki
Context7

Getting Started

Begin by ensuring the package is installed (it ships with Laravel by default). Configure broadcasting in config/broadcasting.php—set your default driver (e.g., pusher, redis, or log for dev) and define connection details. Next, create a BroadcastServiceProvider (if missing) and register channels in its boot() method. For immediate feedback, define a simple event implementing ShouldBroadcast, implement broadcastOn() (e.g., new PrivateChannel('user.1')), dispatch it using event(), and watch logs or browser DevTools for broadcast traffic. The log driver is ideal for initial testing to confirm payload structure and channel routing before moving to a production-grade driver.

Implementation Patterns

  • Event-as-trigger: Model state changes (e.g., OrderShipped, CommentPosted) as ShouldBroadcast events—keep payload lean (IDs, minimal metadata) to avoid bloat.
  • Channel authorization: Use Broadcast::channel() rules in routes/channels.php for granular access (e.g., return $user->id === $id; for private channels). For presence channels, return user metadata (name, ID) to populate join/leave lists.
  • Echo integration: Subscribe frontend listeners using Laravel Echo—Echo.private('orders.{id}')—and bind to event names matching your class (e.g., OrderShippedorder.shipped). Use this. binding in Vue/React to avoid this context loss.
  • Queue-backed broadcasting: Events broadcast via queue (default) for non-blocking delivery—configure queue connection in broadcasting.php and ensure workers (php artisan queue:work) are running to prevent message loss.
  • Hybrid use: Combine with Broadcast::event() for non-event broadcasting (e.g., ad-hoc admin alerts) and Event::dispatchNow() for synchronous tests.

Gotchas and Tips

  • Authorization silently fails: Verify Auth::guard('web') matches your session guard—Auth::guard('api') won’t resolve Auth::user() for web-based sessions. Add @php echo Auth::check() ? 'logged in' : 'guest'; @endphp in a Blade view to debug auth state.
  • Redis quirks: Broadcasting via Redis does not require a Redis server to be running on the broadcasting port—but does require an active queue worker (php artisan queue:work redis). Miss this, and events vanish into the void.
  • Event naming mismatch: Laravel translates class names to dot-notation (e.g., UserRegisteredUser.Registered), but custom event names break Echo bindings. Override broadcastAs() if you need exact event names.
  • Memory/CORS traps: Large payloads trigger ERR_RESPONSE_HEADERS_TOO_BIG or timeouts; compress or minimize broadcast data. For local dev with Pusher/Ably, set PUSHER_APP_HOST and PUSHER_APP_PORT to 127.0.0.1:8080 if proxying, and confirm frontend key/cluster match dashboard.
  • Debugging: Add 'log' => ['driver' => 'log'] in broadcasting.php—every broadcast attempt appears in laravel.log, including 403s and serialization errors. Use dd() inside broadcastOn() or authorization closures only in dev, as it halts queue processing.
  • Extensibility: Override PusherBroadcaster or implement Broadcaster to inject auth tokens, retry logic, or custom headers—e.g., for custom auth proxies or load-balanced broadcast servers.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport