laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 419 forks source link

Event broadcasting with Lumen not working #602

Closed michaeldzjap closed 7 years ago

michaeldzjap commented 7 years ago

I am trying to integrate event broadcasting into a fresh Lumen 5.4.6 project using Homestead, but I am not having much luck so far. I am pretty sure I've set everything up correctly.

If I use sync as the queue driver I get a

Fatal error: Maximum function nesting level of '512' reached, aborting! in /home/vagrant/Code/lumen-test/vendor/illuminate/container/Container.php on line 685

When I use database as the queue driver the job does end up in the jobs table of my db, but it doesn't get processed at all (it seems not even an attempt is made) and no error is thrown.

The steps I took to enable event broadcasting in Lumen:

  1. Set up a queue listener in Homestead using Supervisor. The program I used is:
[program:lumen-test]
process_name=%(program_name)s_%(process_num)02d
command=php /home/vagrant/Code/lumen-test/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=vagrant
numprocs=8
redirect_stderr=true
stdout_logfile=/home/vagrant/Code/lumen-test/storage/logs/lumen-test.log
  1. Registered EventServiceProvider and BroadcastServiceProvider in bootstrap/app.php:
$app->register(App\Providers\EventServiceProvider::class);
$app->register(\Illuminate\Broadcasting\BroadcastServiceProvider::class);
  1. Made sure the queue is up and running by triggering a simple test event (that isn't broadcast). This works fine.

  2. Copied the broadcasting.php config file from a fresh L5.4 project to the config folder of my Lumen project and loaded it in bootstrap/app.php:

$app->configure('broadcasting');
  1. Added the necessary vars in .env:
QUEUE_DRIVER=database
BROADCAST_DRIVER=log

I've included a test project that demonstrates the problem. There is a /test GET route that triggers the event to be broadcast.

As far as I understand, I do not need to set up an event listener in case of a broadcast event right? Since the whole idea is that it is broadcast over a web socket. But since I am using the log driver shouldn't the event simply be logged?

lumen-test.zip

catalinux commented 7 years ago

Have you tried with


QUEUE_DRIVER=sync
BROADCAST_DRIVER=redis 
michaeldzjap commented 7 years ago

But I would have to install Redis for that first no? And set up a websocket server. I know how to do all that, but I'd like to get the most simple case working first before moving on to more complicated setups.

I did try Pusher in my "real" project and that didn't work either. Same thing, events ending up in my jobs table, but not getting processed.

By the way, changing those drivers as per your suggestions throws the same nesting error as I reported previously.

michaeldzjap commented 7 years ago

Got it! Turns out you shouldn't register BroadcastServiceProvider and configure broadcasting.php yourself in bootstrap/app.php. Both of this already is taken care of in src/Application.php of the Lumen source. This pointed me in the right direction.

This is quite confusing I have to say, as I've noticed that for other things it is necessary to do the configuring and registration yourself (e.g. config/auth.php and MailServiceProvider). It wouldn't hurt if the Lumen documentation would be a little more clear on all this...

chimit commented 6 years ago

Hi, @michaeldzjap! And how did you setup auth routes? I mean /broadcasting/auth.

michaeldzjap commented 6 years ago

@chimit I found out that I wasn't able to do that easily without really looking into things. Since I wasted too much time already with getting broadcasting to work I decided it was just not worth to pursue it further. It's a while ago, so can't remember much, but I seem to remember that auth for broadcasting requires a session (or at least it did back then). And Lumen doesn't provide a configured session mechanism out of the box like Laravel does. But maybe you can enable it somehow, since the source for it is there.

Hope you'll be able to figure it out some other way.

chimit commented 6 years ago

Thanks for the answer, @michaeldzjap! It's still possible to implement those routes manually. Echo allows to provide headers in the constructor, so using tokens instead of sessions is possible. Anyway, it would be much better if Lumen provides this by default.

mowangjuanzi commented 5 years ago

The solution is

// bootstrap/app.php
$app->withFacades(false);