afternow

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

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

mindok

What’s happening in Engine.start_process() - is it spawning a new process or running synchronously?

sb8244

sb8244

Author of Real-Time Phoenix

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.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement