I am working on a chat / message function for an application. When a user sends a message, I want a specific event to be triggered and broadcast. Then I want Laravel Echo to listen to the event broadcast channel for the event through Laravel Echo using the driver pusher.
I want the Laravel Echo implementation to be in the Vue component and retrieve the data that was broadcast through the event. Currently, I can’t receive an event for broadcast to my personal channel or only to a channel.
Here is what I did:
1) Added configuration in .env.
2) Added configuration to bootstrap.js:
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'mykey',
encypted: false
});
3) Added authentication check App\Providers\BroadcastServiceProvider(just return true if the user has already registered):
Broadcast::channel('chat', function ($user) {
return \Auth::check();
});
4) App\Providers\BroadcastServiceProvider::class, App/config/app.php.
5) App\Events\UserSentMessage, PrivateChannel chat. Channel. , event() , . broadcast .
6) mounted Vue Vue ( - .., , Laravel Echo atm):
Echo.private('chat').listen('UserSentMessage', (data) => {
console.log(data);
}, (e) => {
console.log(e);
});
:
Echo.channel('chat').listen('UserSentMessage', (data) => {
console.log(data);
}, (e) => {
console.log(e);
});
Vue . , , , , .
, . broadcast(), event().
, Laravel Echo Echo.private(). , private, ( ) private-chat.