lundi 15 août 2022

Laravel v5.5 echo doesn't catch event and pusher doesn't broadcast event

I am currently unable to BOTH listen to events or use the pusher to broadcast events.

Issue

  1. I have the payload messages being generated in laravel log but it doesnt appear on the pusher debugging site.

  2. In terms of receiving the event, the echo listener doesn't get triggered when a message is sent on the pusher debugging site although there I am already subscribed to it. enter image description here

Front End (Echo)

 $(document).ready(function() {
            window.Echo.private('scannedUpdate')
            .listen('scannedQR', (e) => {
                console.log("fight back");
                alert('fight back');
            });
        });

Event

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class scannedQR implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $username;
    public $message;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($username)
    {
        error_log("Made it");
        $this->username = $username;
        $this->message = $username . " has scanned qr code";
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        error_log("Entered broadcast on");
        return new PrivateChannel('scannedUpdate');
        
        // return 'scannedQR';
    }

    public function broadcastAs()
    {
        error_log("In broadcast as");
        return 'scannedQR';
    }
}

Route (Trigger Event)

Route::get('/chatbot/test', function () {
    broadcast(new scannedQR('Cheese'));
    return "Event has been sent!";
});

Laravel Log

local.INFO: Broadcasting [scannedQR] on channels [private-scannedUpdate] with payload:
{
    "username": "Cheese",
    "message": "Cheese has scanned qr code",
    "socket": null
}  

Bootstrap.js

import Echo from 'laravel-echo'

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

window.Lover = "LOVER!";

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'key',
    // cluster: 'mt1',
    cluster:'ap1',
    encrypted: true
});

Channels (Never gets called)

Broadcast::channel('scannedUpdate', function ($user) {
    error_log("Within channel");
    return true;
});


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire