haheute
haheute

Reputation: 2199

Laravel 5.3 - prevent too many notifications?

My laravel 5.3 application creates database notifications for users when a photo was commented or 'liked'.

If something is liked and unliked often, the user would receive lots of notifications although there is, for example, only 1 new Like.

Perhaps the notification function could sleep() for 5 minutes, then check if the Like or Comment still exists, before creating the notification. But then I still must somehow 'lock' it to not have too many notifications created(?).

Is there something already included in laravel (queues, 'lock'-column or table), or another simple solution?

Thanks for advice.

Upvotes: 0

Views: 915

Answers (3)

Victor
Victor

Reputation: 1041

I know that this is an old question, but I found a better approach to resolve this.

The idea is to use the Laravel RateLimitter class with a custom trait RoutesThrottledNotifications in order to rate limit certain notifications an avoid duplications.

This way you just have to set a throttle decay time in each notification that you want to rate limit.

More info and code here: https://scottwakefield.co.uk/writing/rate-limiting-notifications-in-laravel

Upvotes: 0

Marko Sulamägi
Marko Sulamägi

Reputation: 894

You can delay the job in Laravel with delay(): https://laravel.com/docs/5.2/queues#delayed-jobs

And to avoid sending multiple notifications I would use xpuc7o's approach. To make things faster I would store it in memcache and not db.

Upvotes: 1

xpuc7o
xpuc7o

Reputation: 333

I think better solution would be to set an offset somewhere in your code (some class) or in database.

Than before send a notification you can check:

if last notification time + 5 min < current time - send a new notification.

Upvotes: 1

Related Questions