afternow
Live view blocks handle_info during event
Hello
My application (photobooth) has an event trigger that starts a process. This process captures several images (camera) in a row (with a few seconds pause between every image) and should update the frontend (broadcast). This works perfectly if I trigger the process directly in iex. If I push the button in phoenix and trigger the live_view event, It only updates the frontend after the process is completely done.
def handle_event("shot_picture", _value, socket) do
Engine.start_process()
{:noreply, assign(socket, process_step: "Done.")}
end
def handle_info(%{topic: @topic, event: "state_data", payload: payload}, socket) do
{:noreply, assign(socket, %{state_data: payload.state_data})}
end
And this is the command, how the engine broadcasts the state_data.
Frontend.Endpoint.broadcast!("photobooth", "state_data", %{state_data: state_data})
Any idea how I can solve this?
Marked As Solved
afternow
Thank you all for your answers and your thoughts. This is the solution:
def handle_event("shot_picture", _value, socket) do
spawn(Engine, :start_process, [])
{:noreply, assign(socket, process_step: "Done.")}
end
def handle_info(%{topic: @topic, event: "state_data", payload: payload}, socket) do
{:noreply, assign(socket, %{state_data: payload.state_data})}
end
Have a nice day!
Also Liked
mindok
What’s happening in Engine.start_process() - is it spawning a new process or running synchronously?
sb8244
I would check this first. It was the first thing that came to my mind before I saw the code.
LiveViews are just processes at the end of the day. handle_event/3 is a callback invoked by LiveView when it gets a certain message. When the handle_event function runs, it is going to block other messages from processing until it’s done.
This could be a challenge for your situation, because it appears that you want to synchronously block the LiveView, but also get state updates. I would recommend looking at making the process fully asynchronous. That would alleviate this issue because messages would be quickly processed, which minimizes blocked time.
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








