Skip to content

Asynchronous Queues ​

Deliver notifications in the background to ensure fast HTTP response times.


The ShouldQueue Contract ​

Sending notifications across external networks (such as sending multiple emails, delivering SMS via carrier gateways, or invoking webhooks) can add latency to web requests.

To offload delivery to background workers, simply implement the ShouldQueue contract on your notification class:

php
namespace App\Notifications;

use Jengo\Notifications\Contracts\ShouldQueue;
use Jengo\Notifications\Messages\MailMessage;
use Jengo\Notifications\Notification;

class WelcomeUser extends Notification implements ShouldQueue
{
    public function __construct(public array $user)
    {
        parent::__construct();
    }

    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage())
            ->subject('Welcome to Jengo!')
            ->line('Your account is ready to use.');
    }
}

When $user->notify(new WelcomeUser($userData)) is called, the notification is serialized and dispatched directly to your configured queue worker instead of blocking the current HTTP process.


Delaying Notifications ​

You can postpone notification delivery by specifying a delay in seconds using the $delay property:

php
class FollowUpReview extends Notification implements ShouldQueue
{
    /**
     * Delay delivery by 3600 seconds (1 hour).
     */
    public int $delay = 3600;

    // ...
}

Customizing Queue & Connection ​

You can customize which queue or connection processes the notification:

php
class BulkMarketingAlert extends Notification implements ShouldQueue
{
    /**
     * Target queue name.
     */
    public string $queue = 'marketing';

    /**
     * Connection driver (e.g. 'redis', 'database').
     */
    public string $connection = 'redis';

    // ...
}

Worker Execution ​

Ensure your queue workers are running using jengo/queues or standard CodeIgniter queue commands:

bash
# Process jobs using Jengo Queues daemon
php spark jengo:queue work --queue=default,marketing

# Or using CodeIgniter Queue worker
php spark queue:work

Lifecycle Events ​

jengo/notifications triggers system events during the delivery lifecycle:

  • notifications.sending: Fired before a channel attempt (NotificationSending event).
  • notifications.sent: Fired after successful channel transmission (NotificationSent event).
  • notifications.failed: Fired when a driver throws an exception (NotificationFailed event).

You can register listeners in app/Config/Events.php:

php
use CodeIgniter\Events\Events;
use Jengo\Notifications\Events\NotificationFailed;

Events::on('notifications.failed', function (NotificationFailed $event) {
    log_message('critical', "Notification failed on channel [{$event->channel}] for recipient: " . $event->exception->getMessage());
});

Released under the MIT License.