TenTakano

TenTakano

Streams becomes empty in LiveComponent

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!

Marked As Solved

thiagomajesk

thiagomajesk

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

Also Liked

TenTakano

TenTakano

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.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
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

We're in Beta

About us Mission Statement