Increasing the timeout for Shiny Server

andrea.loweandrea.lowe Member, Moderator, Domino Posts: 27 mod

When accessing your published Shiny Server, the app will timeout after the default setting of 60 seconds of inactivity. There are two ways to increase this timeout:

1) If your team has a Shiny Server Pro license then you could set this up within your Domino installation. If you have a license and need assistance, please reach out to our support team. Once you are using Shiny Server Pro you can increase the timeout using the following guide:

http://docs.rstudio.com/shiny-server/#application-timeouts

2) If you are using Shiny Server (non-pro version) then the following workaround may be useful for you. The following workaround allows the app to send data back and forth through the WebSocket every 55 seconds, keeping the connection open. The following code snippets will need to be included with your Shiny app.

In the UI, add the following code:

tags$head(
        HTML(
          "
          <script>
          var socket_timeout_interval
          var n = 0
          $(document).on('shiny:connected', function(event) {
          socket_timeout_interval = setInterval(function(){
          Shiny.onInputChange('count', n++)
          }, 15000)
          });
          $(document).on('shiny:disconnected', function(event) {
          clearInterval(socket_timeout_interval)
          });
          </script>
          "
        )
        ),

Next, add the following line anywhere after the previous script in the same UI file:

textOutput("keepAlive")

And within the server file, add:

output$keepAlive <- renderText({
      req(input$count)
      paste("keep alive ", input$count)
    })

Once these changes have been made, start your Shiny app and ensure it publishes without errors. The app timeout duration should utilize your new configuration.

Comments

  • mmironmmiron Member Posts: 12
    edited March 10

    Hi Andrea,

    Thank you for providing this information. I am experiencing greyout of my app and the above code didnt work for me. The tags$head code ends in a comma, how is this integrated into the app code? Shiny now has an updated way to create apps where both the server and ui are in the same R script. This is outlined here(https://shiny.rstudio.com/articles/build.html) . Can you give a reproducible example of how to implement this fix above?

    Thanks very much,

    Michelle

  • josh.mineroffjosh.mineroff Member, Moderator, Domino Posts: 8 mod

    Hi Michelle,

    I put together this working example with the Domino quick-start project (where the Shiny library is preloaded) to show the syntax for a combined ui and server file. In this case, tags$head and textOutput are being passed to sidebarPanel function, which is why they need to be followed with a comma.

    In my testing, this ran correctly and prevented the greyscreen timeout after 60 seconds.

    server <- function(input, output) {
      output$keepAlive <- renderText({                             ###
        req(input$count)                                           ###
        paste("keep alive ", input$count)                          ###
      })                                                           ##
      output$distPlot <- renderPlot({
        hist(rnorm(input$obs), col = 'darkgray', border = 'white')
      })
    }
    
    ui <- fluidPage(sidebarLayout(sidebarPanel(
      tags$head(                                                   ###vvv
        HTML(
          "
          <script>
          var socket_timeout_interval
          var n = 0
          $(document).on('shiny:connected', function(event) {
            socket_timeout_interval = setInterval(function(){
              Shiny.onInputChange('count', n++)
            }, 15000)
          });
          $(document).on('shiny:disconnected', function(event) {
            clearInterval(socket_timeout_interval)
          });
          </script>
          "
        )
      ),
      textOutput("keepAlive"),                                     ###^^^
      sliderInput(
        "obs", "Number of observations:", min = 10, max = 500, value = 100
      )
    ),
    
    mainPanel(plotOutput("distPlot"))))
    
    shinyApp(ui = ui, server = server)
    

    Josh Mineroff | Field Engineer

  • mmironmmiron Member Posts: 12

    Thanks Josh! My app was no longer greying out, so I didn't need to try this, but I will keep it in mind for the future.

Sign In or Register to comment.