2

How can I force the browser to automatically reload a rails app at 5am each day?

I have a web app that is installed on a wall mounted TV that functions as a sort of live dashboard at our office. It contains information that gets updated only once a day at 4am. Instead of having to refresh the page each day, I would like it to automatically reload the webpage once a day at 5am.

Andrew posted this javascript example that should work, although I am wondering that since this is a Rails 4 app, there might be a better way using Rails to push out the refresh?

Community
  • 1
  • 1
Ryan
  • 10,798
  • 11
  • 46
  • 60

2 Answers2

0

Server can't affect browser state after the page is loaded. Imagine what would happen if that could be done.

One solution for sending the data to the client would be via WebSockets, but AFAIK these aren't yet supported in Rails 4, but are coming in Rails 5.I haven't used the technology with rails, but this is probably the solution: websocket-rails.

If you don't fancy implementing with WebSockets support, only solution left is what you've already found.

Marko Gresak
  • 7,950
  • 5
  • 40
  • 46
  • Thanks. For this simple app, getting WebSockets up and running is a bit overkill. I'll stick to the javascript or META tag solution. Thanks. – Ryan Aug 11 '15 at 00:52
0

You could use a javascript timeout function to run every 24h:

function YourFunction(){
    var dateOne = new Date();
    var dateTwo = new Date(dateOne.getTime() + 86400000); // + 1 day in ms
    //now setting it to 5 AM
    dateTwo.setHours(5);
    dateTwo.setMinutes(0);
    dateTwo.setSeconds(0);
    dateTwo.setMilliseconds(0);

    //getting the delay in ms 
    var timeDelay = dateTwo.getTime() - dateOne.getTime(); // delay in ms until next day 5:00:00 AM

     //preparing for next day's execution:
    setTimeout(function(){ 
    window.location = window.location.pathname;
    }, timeDelay);
}

or you could use the NodeJS runtime so you could push javascript execution from the server.

The Fabio
  • 5,369
  • 1
  • 25
  • 55
  • This code would have to be ran at exactly 5am and every time something crashes or page has to be reloaded, it would have to be ran at 5am again. A better solution would be to calculate timeout time as difference between now and next 5am time. – Marko Gresak Aug 11 '15 at 00:57
  • we can do it in the function itself, I will update the code for you. – The Fabio Aug 11 '15 at 01:03
  • Sorry to sound daft, but what is the *** your code *** portion for? I simply want the page to refresh. FYI, I'm running Rails 4 with jQuery – Ryan Aug 11 '15 at 22:31
  • I thought you would have to execute some javascript a part from the reload.. I have updated the code so it just reloads the page at 5 AM. Just call the function in your page load event. You might have to adjust the "window.location.pathname" part if your page has query strings on it; – The Fabio Aug 12 '15 at 00:36