Laravel event not broadcast

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) {
  // verify that user is recipient of message
  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.

+5
3

.env
, BROADCAST_DRIVER log (try pusher)
, :

php artisan queue:listen
+6

, , , . , QUEUE_DRIVER = sync. . laravel

+2

@materliu: that was a good hint. In my case, I did not create a queue with the working directory installed in the project I'm working on. The use of the circus. The event was fired in code, but the listener was not available. As soon as I start the "php artisan: listen queue", unexpectedly all previous transfers were processed.

It is usually difficult to determine which component of a complex system does not work.

0
source

Source: https://habr.com/ru/post/1664137/


All Articles