TenTakano

TenTakano

I’m currently developing an application using Phoenix LiveView and encountering an issue with handling a large amount of data fetched from the database. I’ve decided to use streams to manage this data due to its volume. To reuse the layout for displaying the data, I am utilizing LiveView Components.

I have designed the components to contain the streams internally. I use send_update/2 from outside the component to send incremental updates, which are intended to be appended within the component. Initially, everything displays as expected. However, when I send additional data later, the contents of the stream seem to reset, causing previously displayed data to disappear from the view.

I have checked socket.assign inside the update/2 of the component, and it appears empty at this point. Are there any restrictions on using streams within LiveView Components, or could there be another cause for this behavior?

Any guidance or suggestions on how to resolve this would be greatly appreciated.

Elixir: 1.17.2-otp-27
Phoenix: v1.7.14
liveview: 1.0.0-rc.6

Here is the code for the component. I’ve omitted the rendering part, but this includes all the function definitions.

defmodule MyAppWeb.DataViewerComponent do
  use MyAppWeb, :live_component

  @impl true
  def render(assigns) do
    ~H"""
    <div class="w-full">
      <%= for {_id, item} <- @streams.items do %>
        ...
      <% end %>
    </div>
    """
  end

  @impl true
  def mount(socket) do
    {:ok, stream(socket, :items, [])}
  end

  @impl true
  def update(%{msg: msg}, socket), do: {:ok, update_stream(socket, msg)}

  @impl true
  def update(assigns, socket), do: {:ok, assign(socket, assigns)}

  defp update_stream(socket, {:update, items}), do: stream(socket, :items, items)
  defp update_stream(socket, {:reset, items}), do: stream(socket, :items, items, reset: true)

Additionally, this is the code for the parent that handles the updates.

  def notify_update(msg) do
    send_update(ImagineWeb.TimelineComponent, id: "timeline", msg: msg)
  end

P.S.: I believe there are no additions, deletions, or state changes occurring in the DOM where this component is included.

Thanks!

Showing Posts 1 to 4

LostKobrakai

LostKobrakai

It might be that your whole LiveComponent is remounted instead of updated. You can check if mount/1 is called again or not.

TenTakano

TenTakano OP

Thank you for your advice. But unfortunately mount/1 is called only once. I’ve checked it with IO.inspect/2.

thiagomajesk

thiagomajesk

Hi @TenTakano! Welcome to the forums :wave:.
Quick question: where is your phx-update="stream" defined?

TenTakano

TenTakano OP

where is your phx-update="stream" defined?

Thank you so much!! I realized my misunderstanding.
Actually, I didn’t write phx-update to the markup. Now, the application works properly.

— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New

Other Trending Topics Top

aaronrussell
Omni 2.0 is out. Omni is a universal Elixir client for LLM APIs: text generation, tool use, structured output and decisions through one A...
New
dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews