Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tamaraR96's avatar

laravel broadcast with pusher sends messages twice

event.php

public $newTask;

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


public function broadcastOn()
{
    return new PrivateChannel('user-' . Auth::id() . '-tasks');
}

channels.php

 Broadcast::channel('user-' . Auth::id() . '-tasks', function ($user, $id)
  {
if ($user->name == "tamara")
    return true;
   });

bootstrap.js

 window.Echo.private('user-' + this.user_id + '-tasks')
.listen('NewTaskAdded', e => {
    console.log("new task");
    console.log(e);
});

and i just called this ones

controller.php

    NewTaskAdded::dispatch("test");

but pusher is receiving to messages and sends it to users

0 likes
6 replies
bobbybouwmann's avatar

How do you know that there are 2 messages send? I don't see anything strange in your code.

tamaraR96's avatar

i got this as result of console.log() ....

new task  

{newTask: "test"} 

 new task

{newTask: "test"}

and in pusher debug console there are also two

ngaiza's avatar

Facing the same issue with Illuminate\Notifications\Events\NotificationSent

1 like
tamaraR96's avatar

hi @raviteja155 @ahmadsiddeeq0 @raviteja155

so actually i didnt change much

this code works fine with me

event.php

<?php

namespace App\Events;

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

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

    public $users; // array of users id

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

    public function broadcastOn()
    {
        $privateChannel = [];

        foreach ($this->users as $id) {
            $privateChannel[] = new PrivateChannel('user-' . $id . '-tasks');
        };

        return $privateChannel;
    }
}

channels.php

Broadcast::channel('user-{id}-tasks', function ($user, $id) {
// some condition to return true 
    return $user->id === $id;

});

bootsrap.js


window.Echo.private('user-' + this.userId + '-tasks')
    .listen('NewTaskAdded', e => {
   
console.log(e);

    });

controller.php

   NewTaskAdded::dispatch($users);

and upgraded laravel

mulai's avatar

I had the same issue. In my case, it was an issue of Illuminate\Notifications\Events\BroadcastNotificationCreated being called twice. I was calling a notification from an event listener. Changing the return value of shouldDiscoverEvents in EventServiceProvider to false solved my issue.

/** * Determine if events and listeners should be automatically discovered. * * @return bool */ public function shouldDiscoverEvents() { return false;//changing to true will make the above registered event listener executed twice }

Please or to participate in this conversation.