Increase Session Timeout

Is it possible to increase the session timeout? It’s a bit too quick for my application, and if you couple that with the inability to save username/passwords in the browser then over the course of a day the relogging back in gets a little tedious (especially when you use silly length passwords like i do).

I’ve not tried keeping it all alive with a timer - trying that now …

The timer is what I’d suggest for now :slight_smile:

2 Likes

Had it running for a while now and it works fine.

For anyone else who’s interested, add a timer to your client form and set its interval to 300 seconds (or whatever). Call a server function that does something (it can be as benign as my example below). This appears to keep the session up and running.

So for example, client side :

def timer_keepalive_tick (self, **event_args):
  # This method is called Every [interval] seconds
  dummy=anvil.server.call('fe_keepalive')

Server side :

@anvil.server.callable
def fe_keepalive():
  return "pong"
3 Likes

Just a side thought - I would also recommend building in a way of terminating the session so it doesn’t stay open indefinitely. There’s no particular problem I have in mind here, but “forever” is usually too long for anything.

The way I would do this is to have the server function return one message for “ok, keep going” and another for “that’s enough now”. In the client side event handler set the time interval to 0 at the start then only reset it back to 300 (or whatever) if you get an “ok” back.

Untested pseudo-example :

def timer_keepalive_tick (self, **event_args):
  self.timer_keepalive.interval=0
  result=anvil.server.call('fe_keepalive')
  if result == "ok":
    self.timer_keepalive.interval = 300

Server side :

@anvil.server.callable
def fe_keepalive():
  #do some check to see how many of these in a row we have received.
  if ok_to_continue:
    return "ok"
  else:
    return "stop it now"

If you do nothing the timer will stop eventually and the session will expire.

1 Like

I gather from this post that setting a timer’s interval property to 0 causes its code to stop running (and a quick test seems to confirm that). It would be helpful to add this to the documentation (unless I have just missed it).

Also, the anvil.server.call_s silent server call function doesn’t appear in the documentation, as far as I have seen.

3 Likes

Thanks for the input @hugetim, those things will appear in the reference docs next time we release.

1 Like

Hello @david.wylie, @shaun, I have implemented the timer solution to increase my session timeout.
I have my timer’s interval set at 120, still I get a Session timeout, please refresh to continue message. Did the timeout interval change? Or what could possibly be the issue?

Does this happen every time, or just once in a while? How long was the delay before the session expired?

Hello @meredydd,

I happens once in a while. The delay was set at 120 seconds.

You are definitely calling a server side function in your timer event?

@david.wylie, isn’t that what I’m suppose to do?

Yeah, I was just checking.

I’ve not tested this for a while, so I’m going to run a test overnight and see if it works.

edit - actually, here’s a question. In your ping/pong are you sending back static data or dynamic data? Reason I ask is I wonder if static data is cached and therefore doesn’t refresh the session like dynamic data might.

Just a thought. One for @anvil that, I think.

edit 2 - ran all night, pinging at 10 minute intervals, with no failures. So broadly speaking that method seems to work for me.

It happens once in a while

That’s inevitable, as we refresh various bits of our infrastructure - a session won’t stay alive for weeks or months at a time. If you do need to be able to handle this, you can add code to handle and recover from expired sessions. Here are the docs explaining how to do that:

1 Like

@meredydd, okay thanks.