I have a laravel app, where users can set up "price watchers" for different assets. I am getting the current prices via API in a scheduled artisan command (lets say every minute) and broadcasting the price changes to all the users via seperate private channels. The problem is that the command keeps running even if there are no users on site. I dont want to spam the API with requests and broadcasting events while there is no present user to see it.
Whats the best approach for this? Should i dispatch a job manually when someone logs in? But how can i make the job keep running as long as the user is there, and how i can end it after? Honestly it confuses me pretty much, am i missing something?
Hope my problem is understandable
I guess you could try running a function every time someone logs in which checks whether the job is active or not. If it's not active you can trigger it.
Everytime someone logs out, you can check how many active sessions/ logged in users exist, if this equates to 0 - kill the job
Not sure this is the most elegant solution but in principle should work
Update
Actually come to think of it, an even cleaner way of doing all of this would be to use the laravel scheduler. Schedule a job to run every minute, and every single time it runs, you will check if there are any active sessions, if not you just ignore, if active sessions exist, then run your logic