spritefullake
Phoenix Liveview using Task.Supervisor.async_nolink vs PubSub for a long worker
I have a long task I need to run to basically compute something realtime as a user scrolls a PDF. What is the best way I should be structuring my calling code for the external api? I started off using async_nolink but I noticed that it seems I can’t name the tasks and there are timeout issues unless I modify the supervisor config. Would using PubSub be appropriate for this usecase? Just begin the external api call from the liveview handle_event and then when the task is completed, send a notification to the PubSub which broadcasts to the liveview. I do not want to block my UI if the task takes too long. In addition, If the user initiates a new demand, I would want the previous one to be abandoned / ignored.
Most Liked
benwilson512
You can use put_private Phoenix.LiveView — Phoenix LiveView v1.2.5 for this since you don’t need change tracking. It also wouldn’t be particularly weird to put it in the assigns, as you might drive some UI behaviour to indicate that a background task is in progress.
Task.Supervisor — Elixir v1.20.2 has some examples of integrating async_nolink with handle_info.
Ultimately you need to store the task ref because it’s the most reliable way to detect crashes too.
tcoopman
An other option might be using async assigns like shown here:
spritefullake
These async assigns on the Phoenix documentation is great, wish it was made more prominent or easier to have found (I guess it is a relatively newer feature).
Also, at what point is the use of a GenServer / seperate module for managing workers warranted vs bare tasks within the liveview callbacks?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








