0
votes

I'm using laravel 5.8 version. I'm sending a message to pusher but put pusher receive event {"message": null}. Here is my broadcasting.php file

'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
//                'cluster' => env('PUSHER_APP_CLUSTER'),
//                'useTLS' => true,
                'cluster' => 'ap2',
                'useTLS' => true
            ],
        ], 

There is my chatevent.php file.

 namespace App\Events
    use Illuminate\Broadcasting\Channel;
    use Illuminate\Foundation\Auth\User;
    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;

class ChatEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $message;
    public $user;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($message, User $user)
    {
        $this->$message = $message;
         $this->$user = $user;
    }

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

There is my bootstrap.js file. import Echo from 'laravel-echo';

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

window.Echo = new Echo({
    // broadcaster: 'pusher',
    // key: process.env.MIX_PUSHER_APP_KEY,
    // cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    // encrypted: truebroadcaster: 'pusher',
    broadcaster: 'pusher',
        key:'REDACTED',
        cluster: 'ap2',
        encrypted: true
});

My controller function where I'm sending message to pusher.

public function send()
    {
        $message = 'hello';
        $user = User::find(Auth::id());

        event(new ChatEvent($message, $user));
    }
}

How to fix it????

1
I have edited your question to remove your API key, however I would recommend that you rotate your app keys to prevent unauthorised access. - doydoy
Can you share the code where you are listening for the event? Is the message content correct when inspected in the Pusher Debug Console (available through the app dashboard)? - doydoy
pusher debug console { "message": null, "user": null } - Ghulam Haider
protected $listen = [ 'App\Events\ChatEvent' => [ 'App\Listeners\ChatListner', ], ]; - Ghulam Haider

1 Answers

2
votes

I think you write the constructor of your event class wrong. Here you put extra $ before message and user variable

    public function __construct($message, User $user)
    {
        $this->$message = $message;
        $this->$user = $user;
    }

It should be like following one.

    public function __construct($message, User $user)
    {
        $this->message = $message;
        $this->user = $user;
    }