Hello! Welcome to the next lesson in our journey to master Laravel.
In our previous session, you learned how to use Eloquent Observers to automatically trigger actions based on a model's lifecycle. This is a powerful pattern for centralizing logic, like invalidating a cache or sending a notification whenever a model is updated.
Today, we'll take that concept a giant leap forward. Instead of just acting within our backend, we will set up the infrastructure to "broadcast" these events directly to the user's browser in real-time. This lesson directly addresses the learning outcome: Configure Laravel for broadcasting events over WebSockets. By the end of this session, you will have a fully configured, real-time communication pipeline ready to power dynamic, live-updating features in your applications.
1. The "Why" of Broadcasting: From HTTP to WebSockets
As a backend developer, you're very familiar with the standard HTTP request-response cycle: a client (like a browser) sends a request, and the server sends back a response. This works great for most web interactions, but it has a limitation: the server can't initiate communication. It can only respond.
So, how do applications like live chats, real-time dashboards, or progress bars work? They can't just have the browser poll the server every second—that's incredibly inefficient. The solution is WebSockets.
A WebSocket connection provides a persistent, two-way communication channel between the client and server. Once the connection is established, the server can push data to the client at any time, without the client needing to ask for it.
Laravel's Broadcasting system is a beautiful abstraction over WebSockets. It allows you to:
- Fire a regular Laravel event on the server.
- Have Laravel "broadcast" this event through a WebSocket driver.
- "Listen" for that event in your client-side JavaScript and update the UI.
To get a solid grasp of these core concepts, let's start with a video that explains them clearly.
Real-Time Event Broadcasting with Reverb in Laravel 11 : Your Complete Guide
This video from Programming Fields will introduce you to the fundamental ideas of broadcasting, WebSockets, and how Laravel's components like Reverb and Echo fit together.
Watch the segment from 01:02 to 07:27. Focus on understanding: What broadcasting is and why it's used for real-time communication. The key difference between the HTTP request-response cycle and a persistent WebSocket connection. The roles of the different components: Reverb (the WebSocket server), and Echo (the JavaScript client library).
2. Choosing Your Driver: Introducing Laravel Reverb
Laravel can broadcast events using several "drivers." Think of these as different backends for handling the WebSocket connections.

While third-party services like Pusher and Ably are powerful, we will focus on Laravel Reverb. Reverb is Laravel's official, first-party WebSocket server.
By choosing Reverb, you are using a tool built by the Laravel team, ensuring seamless integration, high performance, and full control over your application's infrastructure.
3. Server-Side Configuration: Setting Up Reverb
Let's get our hands dirty and configure the backend. The process is streamlined thanks to Laravel's Artisan commands.
Step 1: Install Broadcasting Support
The first step is to run the install:broadcasting command. This will publish the necessary configuration files and prompt you to install a driver.
php artisan install:broadcasting
When prompted, choose Reverb. The command will then:
- Install the
laravel/reverbpackage via Composer. - Publish the
config/reverb.phpandroutes/channels.phpfiles. - Add the necessary environment variables to your
.envfile. - Install the required Node dependencies (
laravel-echoandpusher-js).
For a detailed walkthrough, the official Laravel documentation is your best friend.
Broadcasting - Laravel Documentation
Let's consult the official Laravel documentation for the server-side installation process. This will be our primary reference.
Read the sections titled 'Quickstart' and 'Server Side Installation', focusing specifically on the parts about Reverb. This will reinforce what the Artisan command does and why.
Step 2: Review the Configuration
After running the installation, take a moment to inspect the files that were created or modified.
-
.envfile: You will see several new lines.BROADCAST_CONNECTION=reverb REVERB_APP_ID=your-app-id REVERB_APP_KEY=your-app-key REVERB_APP_SECRET=your-app-secret REVERB_HOST=localhost REVERB_PORT=8080 REVERB_SCHEME=httpBROADCAST_CONNECTION: Tells Laravel to use Reverb as the default driver.REVERB_APP_*: These are credentials used to authorize connections between your client and the Reverb server. The installer generates random values for you.REVERB_HOST&REVERB_PORT: Where Laravel should send broadcast events to. This is the public-facing address of your Reverb server.
-
config/reverb.php: This is the main configuration file for the Reverb server itself. For now, the defaults are fine, but it's good to know this is where you could configure things likeallowed_originsfor security. -
routes/channels.php: This file is crucial for securing your broadcasts. We will dive deep into this in our next lesson when we discuss private channels. For now, just know that it's where you define the authorization logic for who can listen to which channel.
4. Running the Reverb WebSocket Server
Your Laravel application is now configured to broadcast events. But we need to actually run the WebSocket server that will handle the connections.
This is done with another simple Artisan command. Open a new terminal window for this, as the server process is long-running.
php artisan reverb:start
You should see output indicating that the server has started, typically on port 8080.
INFO Server started successfully.
Local: http://0.0.0.0:8080
This server will now sit and wait for WebSocket connections from your frontend.
For more details on running the server, including how to specify a different host or port, refer to the Reverb documentation.
Read the section titled 'Running the Server'. Pay attention to the distinction between REVERB_SERVER_HOST and REVERB_HOST—it's a key concept for deployment.
5. Client-Side Configuration: Setting Up Laravel Echo
With the server running, we need to configure our frontend JavaScript to connect to it. This is where Laravel Echo comes in. Echo is a JavaScript library that makes it painless to subscribe to channels and listen for events.
The install:broadcasting command should have already installed Echo and its dependencies. Now, we just need to make sure it's configured correctly.
Open your resources/js/bootstrap.js file. You should find a new block of code at the bottom, which was added by the installer.
// resources/js/bootstrap.js
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST,
wsPort: import.meta.env.VITE_REVERB_PORT ?? 80,
wssPort: import.meta.env.VITE_REVERB_PORT ?? 443,
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});
Let's break this down:
- We import
EchoandPusher(Reverb uses the Pusher protocol, so we need thepusher-jsclient library). - We create a new
Echoinstance and assign it to the globalwindowobject. broadcaster: 'reverb': Tells Echo to use the Reverb driver configuration.- The
key,wsHost,wsPort, etc., are read from your.envfile via Vite, connecting the frontend configuration to your backend settings.
Finally, make sure your frontend assets are being compiled and the development server is running. In another terminal, run:
npm run dev
6. Verifying the Connection
You've now configured the server, started the Reverb process, and set up the client. How do we know it's working?
We can use the browser's developer tools to verify the WebSocket connection.
Getting Started with Laravel Reverb
This video from the official Laravel channel demonstrates how to check the WebSocket connection in your browser's developer tools. This is an essential debugging skill.
Watch the segment from 05:25 to 05:58. The host opens the DevTools, navigates to the 'Network' tab, filters by 'WS' (for WebSockets), and inspects the connection messages. Follow along with your own application running.
If you open your application in the browser, go to Developer Tools > Network > WS, you should see an active connection to your Reverb server. Clicking on it will show a stream of ping and pong messages, confirming that the connection is alive and healthy.
This is what you're looking for: a '101 Switching Protocols' status and a live stream of messages, indicating a successful WebSocket connection.
Conclusion
Congratulations! You have successfully configured a complete real-time broadcasting pipeline in your Laravel application using Reverb. While we haven't broadcast a specific application event yet, all the foundational plumbing is now in place.
Key Takeaways:
- WebSockets provide a persistent, two-way connection ideal for real-time features.
- Laravel Broadcasting is an abstraction layer that simplifies working with WebSockets.
- Laravel Reverb is the official, self-hosted WebSocket server that offers excellent performance and integration.
- The configuration involves three main parts:
- Server-Side: Running
php artisan install:broadcastingand configuring.env. - Server Process: Running
php artisan reverb:startto listen for connections. - Client-Side: Configuring Laravel Echo in
resources/js/bootstrap.jsto connect to the server.
- Server-Side: Running
- You can verify the connection using your browser's developer tools.
Up Next:
With the configuration complete, our next lesson, "Broadcast events to public and private channels," will be all about putting this pipeline to use. We'll take an event, broadcast it, and see it arrive on the frontend. We will also learn the crucial difference between public channels (for everyone) and private channels (for specific, authenticated users).