afternow

afternow

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?

Showing Posts 1 to 7

mindok

mindok

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

snewcomer

snewcomer

Phoenix Core Team

I’m guessing this has to do with the browser’s single threaded event loop. Generally, we push a message up to the server (handle_event) and await for receive.

A few questions. Is the data streaming back? How large is the data?

For example, when streaming audio through a websocket, it might make sense to throw it in a web worker to avoid blocking the Web Audio API and experiencing interruptions.

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.

afternow

afternow OP

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!

LostKobrakai

LostKobrakai

This won’t cleanup our Engine process when your liveview process exits.

afternow

afternow OP

Not?
How would you do it?

LostKobrakai

LostKobrakai

Unless processes are linked they’ll exist unrelated from each other. You could use spawn_link and link the process, but that would also mean the liveview process might exist if your Engine process crashes (unless it traps exists, which I’m not sure if it already does).

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews