0

I'm working on a Ruby on Rails application with Action Cable. Watching the logs, I've noticed that when I put my phone to sleep that the WebSocket is closed automatically, so further updates are not received. However, on wake (directly to the page, or opening the browser again and giving the tab focus) the connection is immediately re-established.

I want to refresh the page at this moment so that it's up-to-date. The "focus" event is not useful, as it will trigger whenever desktop users bounce between windows or tabs.

What event can I use to reload the page on wake? Something must be available, as Action Cable is reconnecting (though I realize it may be using the focus event as well and simply checking if a connection exists or not).

El Tea
  • 1,206
  • 12
  • 21
  • 1
    https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/Online_and_offline_events – fuzzybear Jan 16 '18 at 17:03
  • Thank you. I didn't know about online and offline events and I learned much from that. But it doesn't appear those trigger on iOS (Chrome or Safari) for phone going to sleep/waking up. Or...if they do, I couldn't determine the magic incantation. – El Tea Jan 16 '18 at 18:20
  • I've got a solution that I think I like better - but it doesn't answer the original question so I'm not posting it. Instead of detecting the browser is awake, just piggyback onto the connected: coffeescript method in the browser's callback from Action Cable. Then put in a new Ajax request getting a data refresh. This is much cleaner anyway, and works for network outages as well. – El Tea Jan 16 '18 at 21:51
  • sounds good, best of luck! – fuzzybear Jan 17 '18 at 00:22

0 Answers0