Keeping track of idle time during app usage

Many of the shiny applications I've built in production are now fairly complex and often demonstrated in team meetings. Often the app will be idle at points during the meeting and it is possible that the app will time out if the discussion goes past the defined timeout setting in the shiny server configuration. I'm curious if there is a way to keep track of the idle time (i.e. no user interaction) in a shiny app? I've been thinking about building an observe or reactivePoll that could keep track of this, but I'm not sure what is the best way to record non-interaction within the app. My goal would be to pop up a simple modal that tells them the timeout limit is about to be reached and press a button to continue.

1 Like

I think you would have to do this all on the client side. The idle timer (or session timeout) in Shiny Server is based on client-server interaction. Any activity on the server (like observe or reactivePoll executing) sends idle/busy messages to the client and resets this idle timer. Polling would keep the app alive forever.

From the client side, you can use Shiny's JavaScript events to track client-server interaction. The shiny:message and shiny:inputchange events would probably cover most cases. So something like:

(function() {
  var timeoutWarningMsecs = 5 * 1000;
  var idleTimer;
  
  function onTimeout() {
    alert('warning: session is about to time out!');
  }
  
  function startIdleTimer() {
    if (idleTimer) clearTimeout(idleTimer);
    idleTimer = setTimeout(onTimeout, timeoutWarningMsecs);
  }
  
  $(document).on('shiny:message shiny:inputchanged', startIdleTimer);
  
})();
4 Likes

This is excellent! I will give this a spin in my apps.