Stop Refreshing! Laravel Echo Hooks for React and Vue

Laravel////3 min read

Overview

Real-time updates transform static web pages into living applications. While previously made this possible through and , the integration on the frontend often required verbose boilerplate to manage listeners and state. The introduction of Echo hooks simplifies this by providing a hook-based approach for and . This allows developers to subscribe to channels and respond to events directly within component logic, eliminating the friction of manual websocket management.

Prerequisites

To follow this guide, you should have a baseline understanding of and . Familiarity with the stack is helpful, as the hooks are designed to work seamlessly within that ecosystem. You will need a local development environment capable of running 11+ and .

Key Libraries & Tools

  • Laravel Reverb: A first-party, high-performance server for Laravel applications.
  • Laravel Echo: The JavaScript library that makes it painless to subscribe to channels and listen for events.
  • echo-react / echo-vue: Specialized packages containing the new hooks (like useEcho and useEchoModel) for modern frontend frameworks.
  • Laravel Idea: A plugin that accelerates development through advanced code generation.

Code Walkthrough

Setting up real-time features starts with the server-side configuration. Use the artisan command to pull in the necessary broadcasting scaffolding:

php artisan install:broadcasting

When prompted, select Reverb as the driver. This command installs the backend dependencies and the frontend packages, including echo-react or echo-vue.

Listening for Events

In a component, you can now use the useEcho hook to subscribe to a private channel and react to a specific event. This replaces the old window.Echo.private().listen() syntax with a more declarative pattern:

useEcho('orders', (echo) => {
    echo.private().listen('OrderStatusUpdated', (event) => {
        console.log('Order Update:', event.order);
        setOrder(event.order);
    });
});

Synchronizing Models

The useEchoModel hook is even more specialized. It listens for standard model events like updates or deletions. To use this, your model must implement the BroadcastsEvents trait and define a broadcastOn method:

namespace App\Models;

use Illuminate\Database\Eloquent\BroadcastsEvents;

class User extends Authenticatable
{
    use BroadcastsEvents;

    public function broadcastOn($event)
    {
        return [new PrivateChannel('App.Models.User.' . $this->id)];
    }
}

On the frontend, the hook tracks the specific model instance:

useEchoModel('App.Models.User', userId, {
    updated: (event) => setUser(event.model),
});

Syntax Notes

When using , you should define interfaces for your event payloads. This ensures that when you access event.order or event.model, your IDE provides full auto-completion. Notice the naming convention for private channels; often expects a dot-notated string (e.g., App.Models.User) which must match exactly between the channels.php routes and the frontend hook call.

Practical Examples

  • Live Notifications: Displaying a toast message when a user receives a new message without requiring a page poll.
  • Order Tracking: Updating a progress bar or status badge on a dashboard as a package moves through shipping stages.
  • Presence Indicators: Showing which team members are currently active on a shared document.

Tips & Gotchas

Always remember that private channels require authorization. If you see a 403 Forbidden error in your browser console, check routes/channels.php. You must define the authorization logic for every private channel. Additionally, ensure your queue worker is running (php artisan queue:work), as broadcasting events are dispatched to the queue by default to maintain application performance.

Topic DensityMention share of the most discussed topics · 17 mentions across 13 distinct topics
24%· products
12%· products
6%· technology
6%· products
6%· products
Other topics
47%
End of Article
Source video
Stop Refreshing! Laravel Echo Hooks for React and Vue

Stop Refreshing! Laravel useEcho Hooks for React & Vue

Watch

Laravel // 21:41

The official YouTube channel of Laravel, the clean stack for Artisans and agents. We will update you on what's new in the world of Laravel, from the framework to our products Cloud, Forge, and Nightwatch.

Who and what they mention most
3 min read0%
3 min read