Phoenix LiveView Stream API for inserting many

After playing around with stream/4 and reading up on all the new LV features, I was tasked at work with making an infinite scroll page. I thought it was the perfect candidate to try this new functionality out.

It was really easy to implement following the docs, the only thing i feel is missing is a way to insert many items to the stream. (think when you reach the bottom of the viewport and you want to load another 20 items).

We only have stream_insert/4 which works with 1 item at a time.

I added this to the project but wanted to know if anyone has been thinking about this as I feel we should have something like this directly from the Stream API.

@type socket :: Phoenix.LiveView.Socket.t()

@doc """
Inserts items or updates existing items in the stream.
See `Phoenix.LiveView.stream_insert/4` for opts.
"""
@spec stream_insert_many(socket(), atom(), list(), keyword()) :: socket()
def stream_insert_many(socket, name, items, opts \\ []) do
  Enum.reduce(items, socket, fn item, acc ->
    Phoenix.LiveView.stream_insert(acc, name, item, opts)
  end)
end

Has anyone else needed to do something like this?
Id gladly work on a PR if this is of interest to the community

6 Likes

That would make sense to me. The function could be called stream_insert_all as well.

I tried to implement regular pagination using streams, where all items are replaced when you change the page. The solution in short was:

  • use dom ID based on index, not based on the item
  • reduce over the new items the same way as you do in your stream_insert_many function
  • to clear out extra items if the new page has less items than the previous one, reduce over the unused indexes to delete those extra items

In that scenario, it would have been useful to have both stream_insert_all and stream_delete_all_by_dom_id

Even more useful would be a stream_reset to delete all items. With that, I wouldnā€™t have to use index-based dom IDs.

Instead of adding new *_all functions, the existing ones could also be updated to accept either a single item or a list of items.

4 Likes

Thank you for mentioning that. Trying to implement sorting and filtering and canā€™t figure it out

2 Likes

emptying a stream is on my todo list, but it didnā€™t make the cut for the last release :slight_smile:

27 Likes

Thank you so for much for doing that!
Iā€™ll make sure to point folks to your reply as this Q continues to popup.

1 Like

Is there anything on the roadmap for inserting many items at a time?

1 Like

Iā€™m also here looking for a way to clear a stream. I figured just recreating it would do that, but got that ā€œhook already definedā€ error.

I also want this feature, but hereā€™s what Iā€™m doing in the meantimeā€¦

  def live_view do
    quote do
      use Phoenix.LiveView,
        layout: {Web.Scholarship.Layouts, :app}

      import Phoenix.LiveView, except: [
        stream_insert: 3,
        stream_insert: 4
      ]

      def stream_insert(socket, name, item_or_items, opts \\ [])

      def stream_insert(socket, name, items, opts) when is_list(items) do
        Enum.reduce(items, socket, fn item, socket ->
          stream_insert(socket, name, item, opts)
        end)
      end

      def stream_insert(socket, name, item, opts) do
        Phoenix.LiveView.stream_insert(socket, name, item, opts)
      end

      unquote(html_helpers())
    end
  end
5 Likes

Very clever to ā€œhijackā€ like that. reminds me of monkey patching stuffs in Rails, but here itā€™s way more under control. I like it!

I also think that if we can handle many inserts it should be with the exact same function stream_insert and not bother with extra function, after all weā€™re in Elixir realm where pattern matching is a thing, isnā€™t it?

1 Like

Yes. Please, only suggest that option :smiling_face:

Any chance stream_reset is in the works? Iā€™ve been using streams the last couple of days and the two big features that appear to be missing are bulk inserts (lower priority since thereā€™s easy work arounds) and stream resets.

In my case I have a list of posts with filters. The issue is that updating a filter requires clearing out any existing results and populating with the new ones, but that doesnā€™t appear to be possible right now. I canā€™t stream_delete since I donā€™t have the old results, and I canā€™t start a new stream since that throws an error.

stream_reset(socket, :posts) would work great, or even having stream empty out existing streams first, allowing you to overwrite the stream with a new one.

Edit: Nevermind, looks like the work is done :slight_smile:

1 Like

Both resetting and bulk inserts are on the main branch on github!

A new version with the features hasnā€™t been released yet but you can point your dependency to the main branch to try them out!

{:phoenix_live_view, github: "phoenixframework/phoenix_live_view", branch: "main"}
8 Likes

For posterity, those who came here looking for a stream_insert_all but canā€™t find it, you just use stream/4:

By default, calling stream/4 on an existing stream will bulk insert the new items on the client while leaving the existing items in place

4 Likes