bodhilogic

bodhilogic

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!

Most Liked

peerreynders

peerreynders

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.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement