Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/syropian/laravel-notification-channel-throttling
Throttle your Laravel notifications on a per-channel basis
https://github.com/syropian/laravel-notification-channel-throttling
laravel notifications rate-limiting throttling
Last synced: 2 months ago
JSON representation
Throttle your Laravel notifications on a per-channel basis
- Host: GitHub
- URL: https://github.com/syropian/laravel-notification-channel-throttling
- Owner: syropian
- License: mit
- Created: 2023-11-18T16:52:41.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-29T17:15:15.000Z (8 months ago)
- Last Synced: 2024-05-02T01:06:23.383Z (8 months ago)
- Topics: laravel, notifications, rate-limiting, throttling
- Language: PHP
- Homepage:
- Size: 257 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
🚦 Laravel Notification Channel Throttling
Throttle your Laravel notifications on a per-channel basis
## Introduction
When sending notifications through multiple channels (say email and SMS), you may want to throttle the number of notifications sent through a specific channel. For example, you could limit the number of SMS notifications sent to a user to 1 per day, and limit emails to 5 per day. This package allows you to configure this easily directly in your notification classes.
## Installation
You can install the package via composer:
```bash
composer require syropian/laravel-notification-channel-throttling
```## Usage
1. Ensure the notification you want to throttle implements `Syropian\LaravelNotificationChannelThrottling\Contracts\ThrottlesChannels`.
2. Implement the `throttleChannels` method. This method should return an array of channels to throttle, and the configuration for each channel. To omit a channel from throttling either omit the channel from the array, or set the value to `false`.```php
use Illuminate\Notifications\Notification;
use Syropian\LaravelNotificationChannelThrottling\Contracts\ThrottlesChannels;class ExampleNotification extends Notification implements ThrottlesChannels {
// ...public function throttleChannels(object $notifiable, array $channels): array
{
/**
* Throttle the mail channel, so that only one
* email notification is sent every 15 minutes
*/
return [
'mail' => [
'maxAttempts' => 1,
'decaySeconds' => 900,
],
'database' => false,
];
}
}
```## Scoping the rate limiter
By default, the [rate limiter](https://laravel.com/docs/rate-limiting) instance used to throttle is automatically scoped to the notification and channel. If you would like to further scope the rate limiter, you may pass a `key` to the channel configuration.
```php
public function __construct(public Post $post) {}public function throttleChannels(object $notifiable, array $channels): array
{
return [
'mail' => [
'key' => $notifiable->id . ':' . $this->post->id,
'maxAttempts' => 1,
'decaySeconds' => 900,
],
'database' => false,
];
}
```In this example we're rate limiting the mail channel, and we're scoping it to a specific combination of a user and a post.
## Testing
```bash
composer test
```## Credits
- [Collin Henderson](https://github.com/syropian)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.