Laravel Notification Facade Queue Response Time

The Laravel docs explain that when dispatching/sending notifcations, you may want to queue them to speed up application response time.

https://laravel.com/docs/5.5/notifications#queueing-notifications

This is exactly what I want to do, however I am calling it using the notification facade rather than the notifiable trait. My concern is the former is bypassing the queue, and I need it to notify a group of users at once.

As it says in the docs:

Alternatively, you may send notifications via the Notification facade.
This is useful primarily when you need to send a notification to
multiple notifiable entities such as a collection of users.

But when I call my Notification via the facade, it doesn’t queue. I know this because when I monitor my network requests and comment out the facade call, my request goes from over 2 seconds (with the notification call) to under .5 seconds (when I comment it out).

Here is the start of my notification class using the queue (NewAsset):

<?php

namespace AppNotifications;

use IlluminateBusQueueable;
use IlluminateNotificationsNotification;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;

class NewAsset extends Notification implements ShouldQueue
{
    use Queueable;

Here is the call:
$asset = new Asset;
$asset->user_id = Auth::user()->id;
$asset->type = "Text";
$asset->content = $content;
$asset->forum_id = 1;
$asset->save();
$users = User::where("id","!=",Auth::user()->id)->get();
Notification::send($users, new NewAsset($asset,Auth::user()));
//if i comment out the notification call above, response time decreases dramatically 
return;

What am I doing wrong?

Oh… it seems it is triggering the queue:

php artisan queue:listen
[2018-03-31 15:48:22] Processing: AppNotificationsNewAsset
[2018-03-31 15:48:22] Processed:  AppNotificationsNewAsset
[2018-03-31 15:48:23] Processing: AppNotificationsNewAsset
[2018-03-31 15:48:23] Processed:  AppNotificationsNewAsset
[2018-03-31 15:48:24] Processing: AppNotificationsNewAsset
[2018-03-31 15:48:24] Processed:  AppNotificationsNewAsset
[2018-03-31 15:48:25] Processing: AppNotificationsNewAsset
[2018-03-31 15:48:25] Processed:  AppNotificationsNewAsset

Why is it so slow then? 🙁

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The Notification::send is slow (> 2 seconds), the chance is, you have thousands of notifiable entities pushing to the database queue, which is slow because thousands of insertion statement executes on database. To improve, you can:

  1. Use other queue drivers, such as Amazon SQS, Beanstalkd, Redis etc.. They are optimized for work queue with low latency. They are lightning fast, when compared to database as a work queue.
  2. Create another job and let the worker queue all the notifications, for example:
    php artisan make:job QueueUserNotificationsJob

    YourController.php
    dispatch(new QueueUserNotificationsJob(Auth::user()->id));

    QueueUserNotificationsJob.php
    public $authUserId = null;
    
    public function __construct($authUserId) {
    
        $this->authUserId = $authUserId;
    
    }
    
    public function handle() {
    
        $users = User::where("id", "!=", $this->authUserId)->get();
    
        Notification::send($users, new NewAsset($asset, $this->authUserId));
    
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x