Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

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

Navigation

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

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
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.

LaravelPHPWebSocketsReal-timeBroadcastingTutorial

Last month, a client asked me to add a live notification system to their dashboard. They were tired of users hitting the refresh button to see if a new order had arrived. I knew immediately that I needed to move away from standard request-response cycles and start using Laravel broadcasting.

If you've been building standard CRUD apps, the jump to real-time events feels like magic. But underneath the hood, it’s just a way for your server to push data to the client the moment something happens.

The Problem with Polling

Before we dive into the code, let’s talk about the "wrong" way to do this. My first instinct years ago was to set up a setInterval in JavaScript that hits an API endpoint every 5 seconds. It worked, but it was inefficient. It hammered the database, added unnecessary load to the server, and felt clunky.

If you are interested in how data flows in more complex systems, you might want to look at how WordPress event-driven architecture handles these streams, but for Laravel, we have a much cleaner path.

Setting Up Your Environment

To get started, you’ll need a driver. Most of us start with Pusher because it’s a managed service that handles the heavy lifting of WebSockets.

  1. Install the Pusher PHP SDK via Composer: composer require pusher/pusher-php-server
  2. Update your .env file with your credentials from the Pusher dashboard.
  3. Set your BROADCAST_CONNECTION to pusher.

Don't forget to uncomment the BroadcastServiceProvider in your config/app.php file. If you skip this, your events will fire, but they’ll never leave your server.

Creating Your First Broadcast Event

Laravel makes this simple. Run php artisan make:event OrderShipped. Inside your new event class, you need to implement the ShouldBroadcast interface.

PHP
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class OrderShipped implements ShouldBroadcast
{
    public $order;

    public function __construct($order)
    {
        $this->order = $order;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('orders.' . $this->order->id);
    }
}

When you fire this event with event(new OrderShipped($order)), Laravel automatically serializes your public properties and sends them off to your configured driver.

Connecting with Laravel Echo

Now that the server is sending events, we need the front end to listen. This is where Laravel Echo shines. It’s a JavaScript library that wraps the complex WebSocket connection logic into a clean API.

Install it via NPM: npm install --save-dev laravel-echo pusher-js.

In your resources/js/bootstrap.js, configure Echo:

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

window.Pusher = Pusher;

window.Echo = new Echo({
    broadcaster: CE9178">'pusher',
    key: import.meta.env.VITE_PUSHER_APP_KEY,
    cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER,
    forceTLS: true
});

Now, in your component, you can listen for the event:

JAVASCRIPT
window.Echo.private(CE9178">`orders.${orderId}`)
    .listen(CE9178">'OrderShipped', (e) => {
        console.log(CE9178">'Order status:', e.order);
    });

Why Real-Time Web Apps Fail

One mistake I see juniors make often is trying to broadcast everything. If you broadcast every single database update, your Pusher bill will skyrocket, and your front end will become a chaotic mess of flickering UI elements.

Be selective. Only broadcast events that the user needs to know about right now. If you're dealing with massive data sets, you might eventually need to look into Laravel Transactional Outbox with Change Data Capture for Consistency to keep your event delivery reliable.

Also, remember that you are sending data over the public internet. Always use private channels for sensitive information. A private channel requires an authorization route in your routes/channels.php file. Never assume that just because a channel is named "private" it is automatically secure—you must define the logic to check if the user has permission to listen.

FAQ

Do I have to use Pusher? No. You can use Laravel WebSockets or Soketi if you want to self-host your own WebSocket server. It’s a great way to save money if you’re comfortable managing the infrastructure.

What if the user is offline? Events aren't stored by default. If a user loses connection, they miss the event. If you need data persistence, you should also Master Laravel Cache: A Beginner's Guide to Performance to fetch the latest state when the user reconnects.

Is this hard to scale? It depends. Pusher scales automatically for you. If you self-host, that’s where the challenge begins. Start with a managed service, and only move to custom infrastructure when your traffic justifies the engineering time.

Final Thoughts

I still remember my first time getting a "Hello World" notification to pop up on my screen without a page refresh. It’s a great feeling. Just remember to keep your events small and your channels secure.

I’m still experimenting with how to handle "reconnection storms" where hundreds of clients try to reconnect to the socket server at once after a deployment. It’s a tricky problem, but that’s the nature of building real-time web systems. Start small, build your first event today, and don't worry too much about the edge cases until you actually hit them.

Back to Blog

Similar Posts

LaravelPHPJune 23, 20264 min read

Mastering Laravel Database Notifications: A Beginner’s Guide

Master Laravel database notifications to store and retrieve user alerts efficiently. Learn how to implement this powerful feature in your PHP application.

Read more
LaravelPHPJune 23, 2026
4 min read

Laravel Eloquent withCount: A Guide to Efficient Counting

Laravel Eloquent withCount helps you avoid N+1 issues when counting related models. Learn how to use it to boost your database performance today.

Read more
LaravelPHPJune 23, 20264 min read

Laravel database transactions: Mastering atomic operations with DB::transaction

Laravel database transactions are vital for keeping your app consistent. Learn how to use the DB facade to ensure atomic operations and protect data integrity.

Read more