Pusher receive the data but not in the Echo callback function (laravel)

Viewed 137

I am using laravel and pusher for in-app notification . What I did is create account in the pusher and do the congifration like this.

bootstrap.js

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');
window.Pusher.logToConsole = true;
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true
});

Events/Comment.php

<?php

namespace App\Events;

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

class Comment implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $username;

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($username)
    {
        //
        $this->username = $username;
        $this->message  = "{$username} liked your status";
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        // return new PrivateChannel('channel-name');
        return new Channel('comments-channal');
    }
}

script.js

$(function() {
            
            const Http = window.axios;
            const Echo = window.Echo;
           
            let channal = Echo.channel('comments-channel');
            channal.listen('Comment', function(data) {
                alert(data);
                console.log(data)
            })
        })

in the controller

Comment::dispatch('fatima');

Then I got the API message in Pusher debug console but in my console I got this

Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"comments-channel"}
Pusher : No callbacks on comments-channel for pusher:subscription_succeeded

FYI I double sheck the .env for BROADCAST_DRIVER=pusher

If there is any help I will be very thankful .

1 Answers

To your Comment broadcast Class You could format your data using this function:

 public function broadcastWith()
    {
        return ['username' => $this->username,'message'=>$this->message];
    }
Related