Stream_insert removes all items from stream

I have a weird issue that I cannot figure out:

I have a basic setup where on mount I retrieve items, and add them to the stream:

    case Event.all_events() do
      {:ok, events} ->
        {:ok,
         socket
         |> stream_configure(:events, dom_id: &"event-#{&1.id}")
         |> stream(:events, events)}

      {:error, _} ->
        {:ok, handle_forbidden_error(socket)}
    end

I’m also subscribed to a PubSub, so when an event is updated, I insert/update it into stream:

  def handle_info(%{topic: "event:updated:status", payload: %{data: data}}, socket) do
    user = socket.assigns.current_user

    case data.user_id == user.id or user.role == "admin" do
      true ->
        {:noreply,
         socket
         |> stream_insert(:events, data, at: 0)}

      false ->
        {:noreply, socket}
    end
  end

For some reason, when I insert the item, all items from the stream disappear, and only this one item is added. If the item already exists on the page, same thing: all items are removed, the item is updated (or a new one is inserted?).

Doesn’t matter if I use at: N or not.

Edit:

Versions:

      {:phoenix_live_view, "~> 1.0"},

      {:phoenix, "~> 1.7.10"},
      {:phoenix_ecto, "~> 4.5"},
      {:ecto_sql, "~> 3.12"},
      {:ecto, "~> 3.12"},

Hi,

do you have phx-update="stream" on the parent HTML tag in your template?

1 Like

Ahhahah :sweat_smile:

Somehow I completely missed that one. Of course that was the reason