Search code examples
laravelredisqueuebeanstalkdbroadcasting

Laravel 5.4 Events fired, but not broadcasted


I have a problem with broadcasting comments on website: - sometimes event listener handled event, but event not broadcasted.

\App\Http\Controllers\CommentController in method "store":

$comment = Comment::create($request->all());
broadcast(new NewCommentAdded($comment));

\App\Events\NewCommentAdded

namespace App\Events;

use App\Models\Comment;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

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

    public $comment;
    public $username;
    public $userimage;
    public $usergroup;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Comment $comment)
    {
        $this->comment = $comment;
        $this->username = $comment->user->name;
        $this->userimage = $comment->user->image;
        $this->usergroup = $comment->user->role_id;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        \Log::info('AddComment event broadcasted');
        return ['webinar_'.$this->comment->webinar_id];
    }

    public function broadcastAs()
    {
        return 'comment';
    }

}

\App\Listeners\BroadcastNewComment

namespace App\Listeners;

use App\Events\NewCommentAdded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class BroadcastNewComment
{
//    use InteractsWithQueue;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param  NewCommentAdded  $event
     * @return void
     */
    public function handle(NewCommentAdded $event)
    {
        \Log::info('Added New Comment Event fired successfully');
    }
}

After all of this i getting logs:

[2017-12-06 16:02:03] production.INFO: Added New Comment Event fired successfully  
[2017-12-06 16:02:03] production.INFO: AddComment event broadcasted  
[2017-12-06 16:03:23] production.INFO: Added New Comment Event fired successfully  
[2017-12-06 16:03:24] production.INFO: AddComment event broadcasted  
[2017-12-06 16:03:58] production.INFO: Added New Comment Event fired successfully  
[2017-12-06 16:17:26] production.INFO: Added New Comment Event fired successfully  
[2017-12-06 16:17:27] production.INFO: AddComment event broadcasted 

As you can see Event Listener handled event: [2017-12-06 16:03:58] production.INFO: Added New Comment Event fired successfully But Event not broadcasted.

My env:

BROADCAST_DRIVER=redis
QUEUE_DRIVER=beanstalkd

My supervisor config:

[program:ta-production-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/domains/data/www/somedomain.com/artisan queue:work --daemon --sleep=3 --tries=5
autostart=true
autorestart=true
user=root
numprocs=5
redirect_stderr=true
stdout_logfile=/var/www/domains/data/www/somedomain.com/storage/logs/worker.log

Solution

  • The solutions of the problem :

    1. Stop using Supervisor or
    2. Downgrade Supervisor or
    3. Upgrade Supervisor.

    I stopped using Supervisor and all works perfectly.