LiveView Component not catching update?

I have a general LiveView component which starts a process in the underlying system and waits for a confirmation to indicate it has been started successfully.

defmodule MyComponent do
  ...

  def confirm_on(id) do
    send_update(__MODULE__, id: id, status: :on)
  end

  def update(%{status: status}, socket) do
    {:ok, assign(socket,
      state: %State{socket.assigns.state |
        status: status})}
  end
end

What happens is that, depending on where the confirm_on() is called, the update is caught or not.

Works:

  def start_system() do
    start_some_lengthy_process()
    MyComponent.confirm_on(:live_id)
  end

Doesn’t Work:

  def start_system() do
    spawn_link(fn ->
      start_some_lengthy_process()
      MyComponent.confirm_on(:live_id)
    end)
  end

What does it mean it doesn’t work?
In both cases, MyComponent.confirm_on() is indeed called and an update is emitted

{:phoenix, :send_update, {MyComponent, %{:live_id, status: :on}}}

However, only in the first case, this update is caught in the update function, in the second case it fails somewhere silently.

Is there a ways to track the progress of the emitted LiveView update?

However, only in the first case, this update is caught in the update function, in the second case it fails somewhere silently.

this is because send_update takes as first parameter the pid of the LiveView process where your component is running. The parameter defaults to the pid of the current process, which is fine if you’re calling send_update from your LiveView process.

if however, as in your second example, you call send_update from another process, this won’t work. In this case you will have to explicitly pass the pid of your LiveView.

1 Like

Indeed that was it! Thank you!