This website is currently under active development (Beta) 🚀. Some features are still work in progress.
Laravel Tutorial

Mastering Laravel Echo: Real-time Communication in Your Laravel Apps

Admin User
Admin User
May 17, 2026
6 min read

Key Takeaways

  • # Mastering Laravel Echo: Real-time Communication in Your Laravel Apps
  • In today's dynamic web landscape, real-time features like live notifications, chat appli...

Mastering Laravel Echo: Real-time Communication in Your Laravel Apps

In today's dynamic web landscape, real-time features like live notifications, chat applications, and interactive dashboards are no longer luxuries but expectations. Laravel, with its robust ecosystem, provides an elegant solution for building such features: Laravel Echo.

Laravel Echo is a JavaScript library that makes it incredibly simple to subscribe to channels and listen for events broadcast by your Laravel application. It acts as a client-side wrapper around WebSockets, allowing you to react to server-side events directly in your frontend application without constant polling.

This comprehensive tutorial will guide you through setting up and utilizing Laravel Echo to bring real-time capabilities to your Laravel projects.

1. Prerequisites #

Before we dive in, ensure you have the following:

  • A working Laravel project (version 8 or higher is recommended).
  • Node.js and npm (or Yarn) installed on your development machine.
  • Composer installed globally.
  • A basic understanding of Laravel events and queues.
  • A WebSocket driver/service (Pusher, Ably, or a self-hosted solution like Redis with Laravel WebSockets).

2. Setting Up Broadcasting in Laravel #

Laravel's broadcasting system integrates seamlessly with Echo. First, we need to configure our backend.

2.1. Configure Broadcast Driver #

Open your .env file and set your BROADCAST_DRIVER. For this tutorial, we'll use Pusher, which is a popular and easy-to-get-started option. If you prefer, you can use Ably, Redis (with laravel-websockets package), or a custom driver.

BROADCAST_DRIVER=pusher

PUSHER_APP_ID=your-pusher-app-id
PUSHER_APP_KEY=your-pusher-app-key
PUSHER_APP_SECRET=your-pusher-app-secret
PUSHER_APP_CLUSTER=your-pusher-app-cluster

Replace the placeholder values with your actual Pusher credentials, which you can obtain by creating a free account on Pusher.com.

2.2. Install Pusher PHP SDK #

If you're using Pusher, install its PHP SDK via Composer:

composer require pusher/pusher-php-server

For Ably, it would be composer require ably/ably-php.

2.3. Uncomment Broadcast Service Provider #

In config/app.php, ensure the App\Providers\BroadcastServiceProvider::class is uncommented. This provider registers the broadcast routes and authorization callbacks.

// config/app.php
'providers' => [
    // ...
    App\Providers\BroadcastServiceProvider::class,
],

3. Installing and Configuring Laravel Echo #

Now, let's set up the client-side JavaScript library.

3.1. Install Echo and WebSocket Client #

Install Laravel Echo and your chosen WebSocket client (e.g., pusher-js for Pusher, ably for Ably, or nothing if using laravel-websockets with default driver).

npm install --save-dev laravel-echo pusher-js
# or for Ably:
npm install --save-dev laravel-echo ably

3.2. Configure Echo in bootstrap.js #

Open resources/js/bootstrap.js (or resources/js/app.js if you prefer to place it there) and uncomment/add the Laravel Echo configuration. Make sure to update it with your BROADCAST_DRIVER and credentials.

// resources/js/bootstrap.js

import Echo from 'laravel-echo';

import Pusher from 'pusher-js';
window.Pusher = Pusher;

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: import.meta.env.VITE_PUSHER_APP_KEY, // or process.env.MIX_PUSHER_APP_KEY for Mix
    cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? 'mt1',
    forceTLS: true
});

// If using Laravel Mix instead of Vite, use process.env.MIX_...
// key: process.env.MIX_PUSHER_APP_KEY,
// cluster: process.env.MIX_PUSHER_APP_CLUSTER,

If you're using Vite, ensure your .env variables are prefixed with VITE_.

3.3. Compile Assets #

Compile your frontend assets to ensure Echo is included in your application's JavaScript bundle:

npm run dev
# or for production:
npm run build

Make sure your Blade layout includes resources/js/app.js or similar.

<head>
    <!-- ... -->
    @vite('resources/js/app.js') <!-- If using Vite -->
    <!-- or <script src="{{ mix('js/app.js') }}"></script> if using Mix -->
</head>

4. Broadcasting Events from Laravel #

Now that Echo is set up, let's create an event and broadcast it.

4.1. Create an Event #

Generate a new event using the Artisan command:

php artisan make:event OrderShipped

4.2. Implement ShouldBroadcast #

Open your OrderShipped event class and implement the ShouldBroadcast interface. This tells Laravel to broadcast this event.

// app/Events/OrderShipped.php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class OrderShipped implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $orderId;
    public $userName;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($orderId, $userName)
    {
        $this->orderId = $orderId;
        $this->userName = $userName;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new Channel('orders'), // Public Channel
            // new PrivateChannel('users.' . $this->userId), // Private Channel example
        ];
    }

    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastAs(): string
    {
        return 'order-shipped';
    }
}
  • ShouldBroadcast: The essential interface that marks an event for broadcasting.
  • $orderId, $userName: Public properties that will be serialized and available to the frontend listener.
  • broadcastOn(): Defines the channels this event will broadcast on. Here, we use a public Channel('orders').
  • broadcastAs(): (Optional) Customizes the event name that Echo will listen for. If omitted, Echo will listen for the class name (e.g., App.Events.OrderShipped).

4.3. Dispatch the Event #

You can dispatch this event from anywhere in your Laravel application, for example, from a controller or service:

// app/Http/Controllers/OrderController.php

namespace App\Http\Controllers;

use App\Events\OrderShipped;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function shipOrder(Request $request, $orderId)
    {
        // Logic to ship the order...
        $userName = $request->user()->name ?? 'Guest';

        event(new OrderShipped($orderId, $userName));

        return response()->json(['message' => 'Order shipped and event broadcasted!']);
    }
}

5. Listening to Events with Laravel Echo #

Now, let's set up our frontend to listen for the OrderShipped event.

5.1. Public Channels (.channel(), .listen()) #

Public channels are open to anyone. No authorization is required. In a JavaScript file (e.g., a Vue component, a plain JS script, or directly in app.js):

// resources/js/app.js or a Vue component method

// Ensure Echo is initialized before listening
if (typeof window.Echo !== 'undefined') {
    window.Echo.channel('orders')
        .listen('order-shipped', (e) => {
            console.log('Order Shipped Event Received:', e);
            alert(`Order #${e.orderId} has been shipped by ${e.userName}!`);
            // Update UI here
        });

    console.log('Listening for order-shipped events on 

FAQs

What is Laravel Echo used for?
Laravel Echo is a JavaScript library that simplifies the integration of real-time functionality into Laravel applications. It allows your frontend to subscribe to channels and listen for events broadcast from your Laravel backend, enabling features like live notifications, chat, and dynamic dashboards.
How do I set up Laravel Echo with Pusher?
To set up Laravel Echo with Pusher, you need to configure `BROADCAST_DRIVER=pusher` in your `.env`, install `pusher/pusher-php-server` via Composer, and then install `laravel-echo` and `pusher-js` via npm. Finally, configure `window.Echo` in `resources/js/bootstrap.js` with your Pusher app key and cluster.
What are the different types of channels in Laravel Echo?
Laravel Echo supports three main types of channels: Public channels (`.channel()`) for open broadcasting, Private channels (`.private()`) for authorized users, and Presence channels (`.join()`) which are a type of private channel that also track and provide information about who is currently subscribed.
Can I use Laravel Echo without Pusher or Ably?
Yes, Laravel Echo can be used without Pusher or Ably. You can use a self-hosted WebSocket solution like `laravel-websockets` (which works with Redis for scaling) by configuring your `BROADCAST_DRIVER` to `redis` and ensuring your Echo configuration points to your custom WebSocket server.

Want more content like this?

Explore more tutorials in the Laravel section.

Explore Laravel

You might also like