Nabnit Jha
Nabnit Jha

Reputation: 347

Laravel Echo not receiving Pusher event in Laravel 8- vujs "^2.5.17" version

I have recived the event details in pusher debug console but I am not able to get any response in my browser (no console log).Plese help me sir/ma'am

I write Echo in app.js

created () {
    window.Echo.channel('privateChat')
        .listen('ChatEvent', (e) => {
            console.log(e);
        });
}

Event broadCasting function

public function sendMessage(Request $request)
{
    $user = Auth::user();
    $message = new Message();
    $message->user_id = $user->id;
    $message->message = $request->message;
    $message->friend_id = $request->friendID;
    $message->my_id = $user->id;
    $message->save();
    // broadcast(new MessageSent($user, $message))->toOthers();
    event(new ChatEvent($request->friendID));
    return ['status' => 'Message Sent!'];
}

BroadCast service provider

class BroadcastServiceProvider extends ServiceProvider
{
public function boot()
{
Broadcast::routes();
Broadcast::routes(['middleware' => ['jwt.auth']]);
require base_path('routes/channels.php');
}
}

Events in chatEvent.php

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

public $friendID;

public function __construct($friendID)
{
    return $this->friendID = $friendID;
}
public function broadcastOn()
{
    return new PrivateChannel('privateChat');
}
}

routes in channels.php

Broadcast::channel('privateChat', function () {
return Auth::check();
});

Laravel-Echo config in aap/js/bootstrap.js

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,
    forceTLS: true,
    auth: {
        headers: {
            Authorization: 'Bearer ' + localStorage.getItem("token")
        },
    },
});

Versions

"laravel-echo": "^1.10.0",
"laravel-mix": "^6.0.6",
"popper.js": "^1.12",
"pusher-js": "^7.0.3",
"vue": "^2.5.17",
"laravel": "8",

enter image description here enter image description here

Upvotes: 2

Views: 2101

Answers (1)

Musti
Musti

Reputation: 510

Instead of using window.echo.channel() use window.echo.private().

Also make sure you have set the correct broadcast driver in .env file. If you are using pusher it should be BROADCAST_DRIVER=pusher after that do php artisan config:clear

Upvotes: 3

Related Questions