#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Event Broadcasting with Pusher and Laravel Echo

Event broadcasting lets server-side events push real-time updates to connected browsers, without the client needing to poll for changes.

The Pieces Involved

PieceRole
PusherHosted WebSocket service that relays broadcast events
Laravel EchoJavaScript library that subscribes to channels in the browser
ShouldBroadcastInterface marking a Laravel event as broadcastable

Making an Event Broadcastable

class OrderShipped implements ShouldBroadcast
{
    public function broadcastOn(): Channel
    {
        return new Channel('orders');
    }
}

Listening in the Browser

Echo.channel('orders')
    .listen('OrderShipped', (e) => {
        console.log(e.order);
    });

Private and Presence Channels

Beyond public channels, Laravel supports private channels (authorized per user) and presence channels (which also track who's currently subscribed) for more controlled real-time features.

Broadcasting covers real-time communication. The next set of topics shifts to code organization patterns that keep larger Laravel applications maintainable.

Setting Up Broadcasting Step by Step

The exact commands differ slightly between Laravel versions, so check the documentation for your version. A typical setup on recent releases looks like this:

  1. Run php artisan install:broadcasting, which creates routes/channels.php, a broadcasting config file and an Echo setup file. Older versions instead required uncommenting the broadcast service provider.
  2. Install the client libraries with npm install laravel-echo pusher-js.
  3. Add your Pusher app credentials to .env.
  4. Make sure a queue worker is running, because broadcast events are queued by default.
BROADCAST_CONNECTION=pusher
PUSHER_APP_ID=your-app-id
PUSHER_APP_KEY=your-app-key
PUSHER_APP_SECRET=your-app-secret
PUSHER_APP_CLUSTER=your-cluster

VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Older projects use BROADCAST_DRIVER instead of BROADCAST_CONNECTION. Only the key and cluster go to the browser; the secret must stay on the server.

Once everything is wired up, a single broadcast travels like this: your code calls OrderShipped::dispatch($order), Laravel places the job on the queue, a worker sends the payload to Pusher over HTTP, Pusher pushes it through open WebSocket connections to every subscribed browser, and Echo runs your JavaScript callback. If any link in that chain is missing, the message silently never arrives, which is why the queue worker is the first thing to check.

Controlling the Payload and Channel Access

By default every public property of the event is sent. You can shape the data and the event name yourself:

class OrderShipped implements ShouldBroadcast
{
    public function __construct(public Order $order) {}

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

    public function broadcastAs(): string
    {
        return 'order.shipped';
    }

    public function broadcastWith(): array
    {
        return ['id' => $this->order->id, 'status' => 'shipped'];
    }
}

A private channel needs an authorization rule in routes/channels.php. Echo calls this rule automatically before subscribing:

Broadcast::channel('orders.{userId}', function ($user, $userId) {
    return (int) $user->id === (int) $userId;
});

In the browser, listen on the private channel. A leading dot tells Echo the name came from broadcastAs:

Echo.private(`orders.${userId}`)
    .listen('.order.shipped', (e) => {
        showToast(`Order ${e.id} has shipped`);
    });

Choosing a Delivery Option

OptionWhen it suits
Pusher ChannelsHosted service, no WebSocket server to run
Laravel ReverbSelf-hosted WebSocket server maintained by the Laravel team
AblyAnother hosted service with a Pusher-compatible mode
PollingVery simple pages where a short delay is acceptable

Because Echo talks to all of these through the same API, switching later mostly means changing configuration rather than rewriting listeners.

Troubleshooting Checklist

  • Nothing arrives: check that a queue worker is running, or implement ShouldBroadcastNow to skip the queue while debugging.
  • 403 on subscribe: the channel rule returned false, or the user is not logged in.
  • Listener never fires: the event name does not match; remember the leading dot for custom names.
  • Sender sees its own update twice: call broadcast(new OrderShipped($order))->toOthers().
  • Sensitive data leaking: use broadcastWith() to send only the fields the browser needs.

Ready to master Laravel Training Course?

Join Uncodemy's hands-on Laravel program and build real, production-ready applications.

Explore Course