Laravel livewire not handle listener from broadcast

  broadcast, laravel, laravel-echo, laravel-livewire

I tried to listen event emited from broadcast in laravel livewire but nothing happen;

on my bootstrap.js i have:

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'myKey',
    wsHost: window.location.hostname,
    wsPort: 6001,
    disableStat: true,
    forceTLS: false,
});

my event laravel

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

    public function __construct()
    {
        //
    }

    public function broadcastAs(){
        return 'task-finish';
    }

    public function broadcastOn()
    {
        return new Channel('TaskTricolor');
    }
}

my listener

class SendTaskNotif
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  TaskFinished  $event
     * @return void
     */
    public function handle(TaskFinished $event)
    {
    }
}

and my livewire component

protected $listeners = ['echo:TaskTricolor,TaskFinished' => 'eventlisten'];

    // public function getListeners(){
    //     return [
    //         'echo:TaskTricolor,TaskFinished' => 'eventlisten'
    //     ];
    // }

    public function start(){
        $this->start = true;
        $tricolors = new TricolorController();
        $this->tricolor = $tricolors->start();
    }

    public function stop(){
        $this->start = false;
        $tricolors = new TricolorController();
        $this->tricolor = $tricolors->stop();
    }
    

    public function emet(){
        event(new TaskFinished());
    }

    public function eventlisten(){
        $vars = "test";
        dd($vars);
    }

on my listener i put dd('test') and when the emet function is called, the dd value is showed;

So when i test with livewire listener handler, nothing happen, my dd not appear, and no error;

I tried to console log Echo from blade to test if i can handle listener, but it show that Echo is not defined, but when i console.log window, i see Echo inside
console.log(window) result

On my websocket dashboard i have this:
websocket dashboard Events

Source: Laravel

Leave a Reply