exjutsu
Can't use Task.async within LiveView?
I’m hoping you can help me to understand a strange issue that I’m experiencing. I’m going through some of the tutorials on the Pragmatic Studio for LiveView and in one of the lessons it illustrates updating the page using :timer.send_interval(1000, self(), :tick) to trigger a handle_info(:tick, socket) call.
The example in the course works just fine, but I wanted to try triggering a handle_info function after some async work had completed, so I tried triggering a Task.async within another button click event that would send pid, :tick at the end of the task…this is where things went sideways. Even if I just try to run a Task.async(fn -> IO.puts "Task running" end) the moment that the task is triggered I get this error:
** (FunctionClauseError) no function clause matching in MyAppWeb.MyAppLive.handle_info/2
Is there something that prevents Task.async from working within a :live_view?
I feel like I’m chasing the wrong problem and that there’s a better way to do what I’m trying to do, which is to have some type of user action trigger a few async and then let the interface update as each task completes.
Any help you can provide would be greatly appreciated.
Marked As Solved
ityonemo
You have three options:
-
the liveview doesn’t directly care about the result of the task or you will take care of sending a result manually (via call, or, in your example by sending directry). In this case use Task.start_link or better, Task.Supervised.start_link instead of Task.async. So this is probably your best option.
-
you care about the result but will handle it in the body of the same block where you issued the task. Then you need to catch the result with Task.await. I don’t think this is what you are looking for.
-
you care about the result but you want to catch the answer outside of the function block. Then you must catch the response message with a handle_info clause. Liveviews don’t come with one by default but it is a valid callback. Guidelines are the same as gen_server (but mind that the liveview handle_info take a socket instead of state): Task — Elixir v1.20.2. I think what is happening is that Task.async is trying to send the message back to your liveview, but you don’t have a catchall handle_info, like:
def handle_info(_, socket), do: {:noreply, socket}
so what happens is it triggers function clause not found when the Task.async shoots back its response.
Also Liked
Supamic
I just wanted to post this shorter ElixirConf talk and accompanying github repo this for anyone else landing her looking for more recent information on this topic because I found it very useful.
NobbZ
A task will always send a message to its creator when it finishes. If I recal correctly, it follows the form {task_id, return_value}, perhaps that one is causing the error? You sadly haven’t shown us the full error message, as well as you haven’t shown us some code how exactly you do it.
NobbZ
A catch all handle_info/2 should also always log at least a warning. Not explicitly handled messages can hint on leaking messages.
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









