Laravel Refresh after every 3 minutes
My website is a prediction app in which users predict a particular number to win the game.
Now My website has game periods and each period is 3 minutes. Now in one day, there are 1440 minutes and 1440/3= 480 which means there are 480 periods in one day.
How I achieve this?
@Dragon_Worrior session mean not login session I mean the game period
Session means GAME PERIOD
@ShamiCanCode sorry dont know about the game session bt I hope it help to you.
@Dragon_Worrior It is login session
If I have understood, you need to refresh some data on the screen every 3 minutes.
You necessarily need to use JS or some related package like Livewire.
Have you tried to install Livewire and to use Livewire polling ?
@vincent15000 yes You understand but on the other hand after every 3 minute period update and particular function run
@ShamiCanCode With Livewire, you can do that.
@ShamiCanCode That is exactly what livewire poll does, It runs a php function after x seconds
@Sinnbeck but I am using laravel blade
@ShamiCanCode Livewire is an extension of Laravel and it uses blade templates too.
Livewire allows you to add reactivity in your application without refreshing the page and you code this only with PHP.
@ShamiCanCode Or you can use plain javascript
setTimeout(function() {
//ajax request to get new data and do stuff with it
}, 3000)
@Sinnbeck but what should I do on server
@ShamiCanCode Write some code that does what you need and return the data to the ajax call?
@ShamiCanCode If you want to install Livewire, it's very simple.
@Sinnbeck this timeout runs every x minutes even someone open page or not
@ShamiCanCode No only if the page is open. If you need to just run some code in the background, then those a called scheduled jobs (or commands) https://laravel.com/docs/9.x/scheduling#main-content
@ShamiCanCode It will execute the function every x minutes only if a user open a page. If you need to setup an update server side, it has to be done another way.
@Sinnbeck Yes this is I am looking for Can I do after every three minutes run a particular controller function in task scheduling
@ShamiCanCode Ok I think that I begin to understand what you need.
On the server you want to execute some function every x minutes and automatically refresh the page for each user.
This can be done as @sinnbeck told you with a scheduled job and then you can broadcast an event to refresh all screens.
@ShamiCanCode You dont run controllers. Those are for http requests. You run a command or a job. But you just schedule it evey 3 minutes
$schedule->call('calculate:stuff')->everyThreeMinutes();
@ShamiCanCode Yes, this can be done with scheduled jobs.
@ShamiCanCode I just participated to the conversation, I let you with @sinnbeck, he is a great developer ;), you will have the best advice with him.
I will go for a walk now, there is a wonderful sun today and it's not too cold.
@Sinnbeck @vincent15000 let me tell you my requirement I want a website which executes a particular function (algorithm) every three minutes. Now I have two scenarios
- User is on that page where results are shown which is done by plain js polling function
- On server every three minutes the algorithm run
@ShamiCanCode You could cache the result using the scheduled job and then each client does polling every 3 minutes and get the cached result. But it can be a bit off, as they dont start 100% the same time. Or you can dispatch a broadcast using websockets which updates all clients (But this is a bit harder to set up)
@Sinnbeck I know about websockets.
task scheduling every three minutes Job executed and when job executed call a websockets to refresh a page.
what do you say?
@ShamiCanCode Yeah but if its not a ton of data, the websocket message could just contain the new data, and just add it to the page using javascript :) No need to reload then
@Sinnbeck Reload is best because it a lot of data
@ShamiCanCode ok. Then be sure to cache it then (or put it in the database). And if the user interacts with the page, its best to use ajax, instead of reload.
@Sinnbeck Yeah sure. You are best <3.
@ShamiCanCode Happy to help :)
$schedule->call(function () {
Check::create([
'check' => 12
]);
})->everyMinute();
It is not creating data in model
@ShamiCanCode did you run the scheduler? https://laravel.com/docs/9.x/scheduling#running-the-scheduler-locally
@Sinnbeck or this I have to buy server with ssh
Please or to participate in this conversation.