Download the PHP package zackaj/laravel-debounce without Composer
On this page you can find all versions of the php package zackaj/laravel-debounce. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zackaj/laravel-debounce
More information about zackaj/laravel-debounce
Files in zackaj/laravel-debounce
Package laravel-debounce
Short Description Debounce jobs ,notifications and artisan commands.
License MIT
Informations about the package laravel-debounce
Laravel debounce
by zackaj
Laravel-debounce allows you to accumulate / debounce a job, notification or command to avoid spamming your users and your app's queue.
It also tracks and registers every request occurrence and gives you a nice report tracking with information like ip address and authenticated user per request.
Table of Contents
-
Introduction
- Features
- Demo
-
Installation
- Prerequisites
- Composer
- Configuration
-
Usage
- Basic usage
- Advanced usage
- Make commands
- No facade usage
- Report Tracking
- Before After Hooks
- Override Timestamp
- Bonus CLI Debounce
- Debugging And Monitoring
- Testing
- Known Issues
- Contributing
- License
Introduction
This laravel package uses UniqueJobs (atomic locks) and caching to run only one instance of a task in a debounced interval of x seconds delay.
Everytime a new activity is recorded (occurrence), the execution is delayed by x seconds.
Features
- Debounce Notifications, Jobs and Artisan Commands Advanced usage
- Report Tracking
- Bonus CLI Debounce
[!WARNING] Debouncing artisan commands requires laravel version >=11
Demo
A debounced notification to bulk notify users about new uploaded files.
https://github.com/user-attachments/assets/b1d5aafd-256d-4f6f-b31a-0d6dc516793b
See Code
FileUploaded.php DemoController.phpInstallation
Prerequisites
- PHP >= 8.1
- Laravel application (>= 10.x)
- Up and running cache system that supports atomic locks
- Up and running queue worker
Composer
Configuration
Optionally publish the config file:
This will publish config/debounce.php:
You can also toggle debouncing via your .env file:
This is useful for local development or testing environments where you want to disable debouncing without changing any code.
Usage
Basic usage
You can debounce existing jobs, notifications and commands with zero setup.
[!NOTE] you can't access Advanced usage.
Advanced usage
In order to use:
- No Facade Usage
- Report Tracking
- before/after Hooks
- Debounce from custom timestamp
your existing jobs, notifications and commands must extend:
or just generate new ones using the available make commands.
Make commands
-
Notification
-
Job
- Command
No facade usage
Alternatively, now you can debounce from the job, notification and command instances directly without using the Debounce facade used in Basic usage
Report Tracking
Laravel-debounce uses the cache to store every request occurrence, use getReport() method within your debounceables to access the report chain that has a collection of occurrences.
Every report will have one occurrence minimum.
Before After Hooks
If you wish to run some code before and/or after firing the debounceables you can use the available hooks.
Important: after() hook could run before your debounceable is handled if it's sent to the queue when:
sendNow==falseand your notificationimplements ShouldQueuesync==falseand your jobimplements ShouldQueuetoQueue==true(command)
see: Basic usage
Debounce job
Debounce notification
You get the $notifiables injected into the hooks.
Debounce command
Due to limitations, the hook methods must be static.
Override Timestamp
By default laravel-debounce debounces from the last occurrence happenedAt timestamp
You can override this method in your debounceables in order to debounce from a custom timestamp of your choice. If null is returned the debouncer will fallback to the default implementation above.
Debounce job
Debounce notification
You get the $notifiables injected into the method.
Debounce command
Due to limitations, the method must be static.
Bonus CLI Debounce ( Laravel version >= 11.0.0 )
For fun, you can actually debounce commands from the CLI using the debounce:command Artisan command.
here's the signature for the command:
php artisan debounce:command {delay} {uniqueKey} {signature*}
Debugging And Monitoring
I recommend using Laravel telescope to see the debouncer live in the queues tab and to debug any failures.
Testing
When running tests, you may want to disable debouncing so jobs, notifications and commands are fired immediately without any debounce logic.
Add this to your phpunit.xml or .env.testing:
Or disable it per test:
Known Issues
1- If you clear / flush the cache, the report tracking and the registered dispatches will be lost.
2- Debouncing artisan commands requires laravel version >=11
Contributing
Contributions, issues and suggestions are always welcome! See contributing.md for ways to get started.