Dead-man Timer

I’ve been playing with GenServer timers and what I would like to do is run a GenServer timer that, when it processes the handle_info() call, causes the web application to redirect to another page. A bonus would be for the redirect to happen only if the user has not interacted with the page since the timer started (i.e. clicking on a button or editing a field resets the timer back to time zero).

My problem is that the redirection requires the conn ‘object’ as one of the parameters but the GenServer, as far as I can see, has no way to obtain the conn ‘object’.

Is there a way to get hold of the conn ‘object’ from within the GenServer ‘processing’?

Or am I doing it completely wrong?

Thanks for taking the time to answer!

If the user doesn’t interact with the page then there is no HTTP request.

  • No HTTP request - no redirect.

So unless you are using LiveView there isn’t anything you can do. The timer would have to be based on the client-side (i.e. the browser) and make a request when there is no activity.

With LiveView you would simply change what is currently rendered on the page after the timeout expires - but there is no redirect involved.

EDIT: I forgot about channels - i.e. you might be able to use them to have the server send a message (at timeout) to the browser and have some browser-based code take some action.

1 Like