Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

Subscribe to the newsletter

Get new articles and course lessons delivered to your inbox. No spam, unsubscribe anytime.

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 23 of the Intermediate Laravel: Real-World Application Patterns course
LaravelJune 26, 20263 min read

Real-time Notifications with Broadcasting in Laravel

Master real-time notifications with Laravel broadcasting. Learn to configure WebSockets, create broadcast events, and sync your UI using Laravel Echo.

LaravelWebSocketsBroadcastingReal-timeEchoReverbphpbackend

Previously in this course, we explored Introduction to Laravel Events and Listeners for Clean Code to decouple our business logic. In this lesson, we are taking that concept a step further by pushing those events directly to the client's browser, enabling real-time UI updates for our project board.

Understanding Real-time Broadcasting

In a standard HTTP request-response cycle, the client must poll the server to see if anything has changed. This is inefficient. With websockets and broadcasting, the server pushes data to the client the moment an event occurs.

Laravel handles this complexity by providing an abstraction layer. You trigger an event in your backend, and Laravel’s broadcasting system sends it to a driver (like Pusher or a local Soketi/Reverb server), which then pushes it to the connected client.

Configuring Broadcasting

First, ensure your broadcasting.php config file is set up. For local development, many developers prefer Laravel Reverb, which is a first-party, high-performance WebSocket server.

  1. Install Reverb: php artisan install:broadcasting
  2. Update your .env file to use BROADCAST_CONNECTION=reverb.
  3. Start the server: php artisan reverb:start.

This creates a persistent connection between your client and server, allowing for near-instant communication.

Creating Broadcast Events

To make an event broadcastable, it must implement the ShouldBroadcast interface. Let’s evolve our TaskCreated event from our project board project.

PHP
namespace App\Events;

use App\Models\Task;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;

class TaskCreated implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;

    public function __construct(public Task $task) {}

    public function broadcastOn(): Channel
    {
        #6A9955">// Broadcast to a private channel scoped to the project
        return new \Illuminate\Broadcasting\PrivateChannel('project.' . $this->task->project_id);
    }
}

By implementing ShouldBroadcast, Laravel will automatically queue this event. When the event is dispatched, it serializes the Task model and sends it to the configured WebSocket driver.

Implementing Client-Side Listeners

Now that the backend is broadcasting, we need the frontend to listen. We use Laravel Echo, the companion library for interacting with these broadcasts.

Assuming you have Laravel Echo installed (via npm install laravel-echo pusher-js), add this to your JavaScript entry point:

JAVASCRIPT
import Echo from CE9178">'laravel-echo';
import Pusher from CE9178">'pusher-js';

window.Echo = new Echo({
    broadcaster: CE9178">'reverb',
    key: import.meta.env.VITE_REVERB_APP_KEY,
    wsHost: import.meta.env.VITE_REVERB_HOST,
    forceTLS: false,
});

// Listening for the event
window.Echo.private(CE9178">`project.${projectId}`)
    .listen(CE9178">'TaskCreated', (e) => {
        console.log(CE9178">'New task received:', e.task);
        // Update your UI state here(e.g., append to the task list)
    });

Hands-on Exercise

  1. Modify your existing TaskService from Service-Oriented Task Management: Building Robust Business Workflows to dispatch the TaskCreated event after a task is saved.
  2. Open your browser console and the terminal running php artisan reverb:start.
  3. Trigger a task creation through your API and verify that the JSON payload appears in the client-side console.

Common Pitfalls

  • Forgetting to dispatch the event: Broadcasting only works if the event is actually dispatched. Ensure you are calling event(new TaskCreated($task)) in your service layer.
  • Authorization Failures: If you are using PrivateChannel, your routes/channels.php must define who is allowed to listen. If you don't return true in the callback, the client will never connect.
  • Queue Driver: Broadcasting relies on the queue system. If your QUEUE_CONNECTION is set to sync, your broadcast might feel slow or fail if the WebSocket server isn't responsive. Use redis or database for production-like behavior.

Recap

We've bridged the gap between our backend events and the client UI. By configuring Reverb, implementing ShouldBroadcast on our events, and using Laravel Echo to listen on private channels, we have enabled true real-time capabilities in our project board. This eliminates the need for manual page refreshes and creates a polished, reactive experience for our users.

Up next: We will explore Job Chaining and Batching to handle complex, multi-step background processes efficiently.

Previous lessonHandling File Uploads in REST APIsNext lesson Using Observers for Model Lifecycle Hooks
Back to Blog

Similar Posts

LaravelPHPJune 23, 20264 min read

Laravel Broadcasting: A Beginner’s Guide to Real-Time Web Events

Master Laravel broadcasting to build interactive real-time web applications. Learn how to use the Broadcast facade, Pusher, and Echo for instant updates.

Read more
LaravelJune 28, 20263 min read

Handling Webhooks Securely: Validation and Queueing in Laravel

Part of the course

Intermediate Laravel: Real-World Application Patterns

intermediate · Lesson 23 of 58

  1. 1

    Architecting for Maintainability

    3 min
  2. 2

    Implementing the Service Layer

    3 min
  3. 3

    Repository Pattern Fundamentals

    3 min

Learn to build production-ready integrations by validating webhook signatures and offloading processing to queues to ensure security and system reliability.

Read more
LaravelJune 28, 20264 min read

Advanced Database Migration Strategies for Laravel

Master non-breaking migrations and safe rollback procedures. Learn the expand-and-contract pattern to evolve your database schema without production downtime.

Read more
  • 4

    Project Board Domain Modeling

    3 min
  • 5

    Advanced Eloquent Scopes and Accessors

    4 min
  • 6

    Service-Oriented Task Management

    3 min
  • 7

    REST API Fundamentals with Sanctum

    3 min
  • 8

    Resource Controllers and API Responses

    3 min
  • 9

    Handling API Validation and Form Requests

    3 min
  • 10

    Implementing Middleware for API Security

    4 min
  • 11

    Database Transactions for Data Integrity

    3 min
  • 12

    Error Handling and Global Exceptions

    3 min
  • 13

    Introduction to Laravel Events and Listeners

    3 min
  • 14

    Asynchronous Processing with Queues

    4 min
  • 15

    Job Chaining and Batching

    3 min
  • 16

    Feature Testing Fundamentals

    4 min
  • 17

    Mocking Services and Repositories in Tests

    3 min
  • 18

    Testing Events and Jobs

    3 min
  • 19

    Database Factories and Seeding

    3 min
  • 20

    API Versioning Strategies

    4 min
  • 21

    Advanced Request Filtering and Sorting

    3 min
  • 22

    Handling File Uploads in REST APIs

    3 min
  • 23

    Real-time Notifications with Broadcasting

    3 min
  • 24

    Using Observers for Model Lifecycle Hooks

    3 min
  • 25

    Implementing Policies for Authorization

    3 min
  • 26

    Customizing Authentication Guards

    3 min
  • 27

    Rate Limiting API Endpoints

    4 min
  • 28

    Eloquent Performance Optimization

    4 min
  • 29

    Caching Strategies for Performance

    4 min
  • 30

    Using Traits for Code Reuse

    3 min
  • 31

    Advanced Dependency Injection with Service Providers

    3 min
  • 32

    Command Line Tools with Artisan

    3 min
  • 33

    Scheduled Tasks and Cron Jobs

    3 min
  • 34

    Integrating Third-Party Services

    3 min
  • 35

    Handling Webhooks

    3 min
  • 36

    Logging and Monitoring

    3 min
  • 37

    Database Migrations Best Practices

    3 min
  • 38

    Advanced Testing: Integration Tests

    4 min
  • 39

    Testing API Authentication

    4 min
  • 40

    Code Quality and Static Analysis

    3 min
  • 41

    Project Structure for Large Applications

    3 min
  • 42

    Environment and Configuration Management

    3 min
  • 43

    Deploying Laravel Applications

    4 min
  • 44

    Database Indexing Strategies

    4 min
  • 45

    Using Value Objects

    4 min
  • 46

    Strategy Pattern for Business Rules

    3 min
  • 47

    Advanced Queue Monitoring

    3 min
  • 48

    Building a Search API

    3 min
  • 49

    Handling Concurrency and Race Conditions

    4 min
  • 50

    API Documentation with OpenAPI

    3 min
  • 51

    Testing with Test Doubles

    3 min
  • 52

    Implementing Multi-Tenancy

    4 min
  • 53

    Refactoring Legacy Code

    4 min
  • 54

    Using Middleware for Feature Flags

    3 min
  • 55

    Building Reusable Packages

    4 min
  • 56

    Performance Profiling

    3 min
  • 57

    Secure API Design

    3 min
  • 58

    Event Sourcing Concepts

    4 min
  • View full course