php
42 lines · 5 steps
How a broadcast event works in Laravel
A Laravel event pushes order status changes to a private WebSocket channel with a controlled payload.
Explained by
highlit
1<?php
2
3namespace App\Events;
4
5use App\Models\Order;
6use Illuminate\Broadcasting\Channel;
7use Illuminate\Broadcasting\InteractsWithSockets;
8use Illuminate\Broadcasting\PrivateChannel;
9use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10use Illuminate\Foundation\Events\Dispatchable;
11use Illuminate\Queue\SerializesModels;
12
13class OrderStatusUpdated implements ShouldBroadcast
14{
15 use Dispatchable, InteractsWithSockets, SerializesModels;
16
17 public function __construct(public Order $order)
18 {
19 }
20
21 public function broadcastOn(): array
22 {
23 return [
24 new PrivateChannel('orders.'.$this->order->customer_id),
25 ];
26 }
27
28 public function broadcastAs(): string
29 {
30 return 'order.status.updated';
31 }
32
33 public function broadcastWith(): array
34 {
35 return [
36 'id' => $this->order->id,
37 'status' => $this->order->status,
38 'total' => $this->order->total_amount,
39 'updated_at' => $this->order->updated_at->toIso8601String(),
40 ];
41 }
42}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Implementing ShouldBroadcast turns a plain event into one that streams over WebSockets when dispatched.
- 2broadcastWith lets you send a lean, curated payload instead of the whole serialized model.
- 3Private channels scoped to a user id keep real-time updates authorized and per-customer.
Related explainers
rust
use axum::{ extract::ws::{Message, WebSocket, WebSocketUpgrade}, response::IntoResponse, };
How an Axum WebSocket echo server works
websockets
async streams
protocol handling
Intermediate
8 steps
php
<?php final class UserRepository {
A safe PDO user repository in PHP
prepared-statements
repository-pattern
sql-injection
Intermediate
7 steps
php
<?php namespace App\Services;
Caching tenant dashboard metrics in Laravel
caching
multi-tenancy
aggregation
Intermediate
7 steps
php
<?php namespace App\Http\Controllers\Auth;
Rate-limited login in Laravel
authentication
rate-limiting
validation
Intermediate
9 steps
php
<?php namespace App\Http\Controllers;
Streaming a large CSV export in Laravel
streaming
csv-export
memory-efficiency
Intermediate
9 steps
php
public function importUsers(array $users): int { $inserted = 0;
Bulk-inserting users with batched PDO upserts
batching
bulk-insert
prepared-statements
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-broadcast-event-works-in-laravel-explained-php-fcd0/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.