Laravel event not being Broadcast
Asked Answered
S

5

11

I am working on a chat/message feature for an app. When a User sends a message, I want a specific Event to be triggered and to be broadcasted. I am then wanting Laravel Echo to listen on the event's broadcast channel to an event via Laravel Echo, using the pusher driver.

I am wanting the Laravel Echo implementation to be in a Vue component and to retrieve the data that was broadcasted via the event. Currently, I can't get an event to broadcast to either a private channel or just a channel.

Here is what I have done:

1) Added the config to .env.

2) Added config to bootstrap.js :

window.Echo = new Echo({
  broadcaster: 'pusher',
  key: 'mykey',
  encypted: false
});

3) Added auth check in App\Providers\BroadcastServiceProvider (just returning true if User is signed in for now):

Broadcast::channel('chat', function ($user) {
  // verify that user is recipient of message
  return \Auth::check();
});

4) Added App\Providers\BroadcastServiceProvider::class, to App/config/app.php.

5) Created the Event App\Events\UserSentMessage, which broadcasts on a PrivateChannel named chat. I have also tried to broadcast to a Channel. I checked and the event is being fired from a call to the event() helper in a controller, it just seems to not be broadcast. I have also tried using the broadcast method in the controller mentioned.

6) Added the following within the mounted Vue life cycle hook in a valid Vue component (component is rendering data etc as expected, just not working with Laravel Echo atm):

    Echo.private('chat').listen('UserSentMessage', (data) => {
                console.log(data);
            }, (e) => {
                console.log(e);
            });

Have also tried:

     Echo.channel('chat').listen('UserSentMessage', (data) => {
                console.log(data);
            }, (e) => {
                console.log(e);
            });

The Vue component is successfully subscribing to the channel specified. The pusher debug console recognized that it is being subscribed to, but the Event, when triggered never gets broadcasted.

Seems to be that there is a problem with broadcasting the event. I have also tried using the broadcast() helper, rather than the event() helper.

Also, when subscribing to the event with Laravel Echo via Echo.private(). It seems to prefix the channel name with private, so I have also tried broadcasting (in the event) to a channel named private-chat.

Stilliform answered 19/12, 2016 at 8:3 Comment(0)
C
41

I can recommend You to check Your .env file
and make sure BROADCAST_DRIVER is not log (try pusher)
and also don't forget to keep running queue listener:

php artisan queue:listen

UPD.: if You look for more advanced solution, read about laravel horizon

Charolettecharon answered 21/12, 2016 at 4:25 Comment(5)
After pulling my hair for hours you saved my day. I did not realize it was all being stacked up in the queue. Thanks!!Cudbear
@StoneyEagle happy to know that my answer was helpfulCharolettecharon
Hi, please why do i have to run php artisan queue:listenCamera
@MamadouHadyBarry from command line. MacOS, Linux: terminal, Windows: cmd or power shellCharolettecharon
@MamadouHadyBarry as an example watch this: youtu.be/EZqNu9Bqp7wCharolettecharon
N
7

In my case, my event class file is generated by "make:event", it does have broadcastOn method... But have no implements ShouldBroadcast~ It causes the laravel-echo-server output nothing~

Laravel Framework 8.13.0

Navicert answered 24/11, 2020 at 6:46 Comment(0)
B
6

the event will not be broadcasted immediately, it will be send to the queue, only when you listen or work on the queue, the event will be consumed. If you want execute jobs immediately, set QUEUE_DRIVER=sync . see: laravel queue

Blush answered 7/4, 2017 at 11:39 Comment(0)
P
1

I have upvoted Num8er's reply but i also found some other issues so maybe they can help you too.

1) Remove all 'private-' from the beginning of your channel names. These get handled behind the scenes by the various libraries (eg socket.io/laravel-echo). If you look at any debug output you will see 'private-' prepended to the front of the channel names but you DO NOT need to add these.

2) If you're using redis then make sure the REDIS_PREFIX is set to an empty string in your .env file. By default it's set to something like 'laravel_database' and this messes up the channel names.

REDIS_PREFIX=

3) Make sure you've got your laravel-echo server running (and redis/pusher). Also you need to make sure you have the queue worker running:

php artisan queue:work

4) It's worth enabling debugging output on your Echo Server as this will tell you useful info about traffic (eg channel names) so modify the laravel-echo-server.json file (in your project root dir) so that devMode is set to true:

"devMode": true,
Pheni answered 23/3, 2020 at 4:5 Comment(0)
F
0

@materliu: that was a good hint. In my case i did not create a queue with the working directory set to the project i am working on. Using circus. The event was triggered in code but no job listener was available. As soon as i run "php artisan queue:listen" suddenly all previous broadcasts have been processed.

Usually its difficult to figure out which component of a complex system does not work.

Fossa answered 4/12, 2018 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.