Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Livewire Throttling Laravel Package

f1uder/livewire-throttling

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require f1uder/livewire-throttling
    

    Publish the config (if needed) with:

    php artisan vendor:publish --provider="Nrox\LivewireThrottling\LivewireThrottlingServiceProvider"
    
  2. First Use Case: Add the ThrottlingTrait to a Livewire component and apply rate limiting to a method:

    use Nrox\LivewireThrottling\ThrottlingTrait;
    
    class MyComponent extends Component {
        use ThrottlingTrait;
    
        public function submitForm() {
            $this->rateLimit('10'); // 10 requests per minute
            // Rest of the logic...
        }
    }
    
  3. Key Files to Review:

    • config/livewire-throttling.php (for customization)
    • resources/lang/en/json (default error messages)
    • app/Http/Livewire/ (your components)

Implementation Patterns

Core Workflows

  1. Basic Rate Limiting:

    public function action() {
        $this->rateLimit('5'); // 5 requests per minute
        // Protected logic
    }
    
  2. Custom Callback Handling:

    public function sensitiveAction() {
        $this->rateLimit('3', function ($seconds) {
            $this->dispatch('throttled', seconds: $seconds);
        });
    }
    
  3. Dynamic Rate Limits:

    public function dynamicAction() {
        $limit = auth()->user()->throttle_limit ?? '10';
        $this->rateLimit($limit);
    }
    
  4. Clearing Limits:

    public function resetThrottle() {
        $this->clearRateLimit();
    }
    

Integration Tips

  • User-Specific Limits: Store limits in the user model (e.g., user.throttle_limit) and pass dynamically.
  • Global Limits: Use middleware to apply package-wide defaults:
    // app/Http/Middleware/ThrottleLivewire.php
    public function handle($request, Closure $next) {
        if ($request->wireable) {
            $request->wire('component')->rateLimit('5');
        }
        return $next($request);
    }
    
  • Localization: Extend language support by adding custom JSON files in lang/{locale}/json.

Gotchas and Tips

Pitfalls

  1. Session Dependency:

    • Throttling relies on the user’s session. Ensure session() is available (e.g., avoid API-only setups).
    • Fix: Use session()->put() explicitly if needed.
  2. Cache Invalidation:

    • Clearing limits (clearRateLimit()) only affects the current session. For shared limits (e.g., IP-based), extend the trait or use Redis.
  3. Error Handling:

    • Default errors use abort(429). Override with custom callbacks or middleware to log/throttle gracefully.
  4. Rate Limit Format:

    • Use X/Y syntax for custom intervals (e.g., rateLimit('5/10s') for 5 requests per 10 seconds). Default is per minute.

Debugging

  • Check Storage: Inspect session()->get('livewire_throttling') to debug stored limits.
  • Logs: Add Log::debug('Throttle check:', ['key' => $this->getThrottleKey()]) in the trait for visibility.

Extension Points

  1. Custom Keys: Override getThrottleKey() to use user-specific or IP-based keys:

    protected function getThrottleKey() {
        return 'user_' . auth()->id() . '_' . $this->methodName();
    }
    
  2. Storage Backend: Replace the session storage with Redis by binding a custom ThrottleStore:

    $this->app->bind(ThrottleStore::class, function () {
        return new RedisThrottleStore();
    });
    
  3. Event Listeners: Dispatch events for throttling (e.g., Throttled, ThrottleCleared) by extending the trait:

    event(new Throttled($this, $seconds));
    

Config Quirks

  • Default Limit: Set in config/livewire-throttling.php (default_limit). Overrides per-method limits if not specified.
  • Time Unit: Default is minutes ('m'). Use 's' (seconds), 'h' (hours), or custom strings (e.g., '10s').
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel