Broadcasting

Contents
Events over websockets
Broadcast event sketch
Listen on frontend
Driver note

Events over websockets

HTTP answers only when asked: the browser sends a request and Laravel returns a response. Real-time features flip the direction - when an order ships on the server, every open dashboard learns about it within a second, with nobody pressing refresh.

Broadcasting is the bridge: your code fires an event, Laravel pushes it through a websocket driver, and JavaScript listeners update the page. Think of it as a radio station where the server transmits and browsers tune in.

Broadcast event sketch

A broadcast event is a plain PHP class that carries public data and names the channel it belongs to. The ShouldBroadcast marker tells Laravel to push it to the socket layer instead of handling it only inside the current process.

namespace App\Events; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class OrderShipped implements ShouldBroadcast { public function __construct(public int $orderId) { } public function broadcastOn(): array { return ["orders"]; } }

Listen on frontend

On the browser side a tiny JavaScript sketch subscribes to the same channel name and reacts when the event arrives. Channel names must match letter for letter, or the listener waits in silence.

Echo.channel("orders").listen("OrderShipped", (e) => { console.log("Shipped order " + e.orderId); });

Driver note

During development use the log driver, which writes every broadcast to the log file instead of opening sockets. The event code stays identical - only the driver setting changes when real traffic needs real websockets.

Keep the channel names stable from day one. Renaming a channel later means updating the event class and every JavaScript listener at the same time, which is exactly the kind of flag day worth avoiding.

Next: Full-text search

Article author: Arthur Isaev

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: