tomkonidas

tomkonidas

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

Most Liked

chrismccord

chrismccord

Creator of Phoenix

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

27
Post #4
zolrath

zolrath

Both resetting and bulk inserts are on the main branch on github!
https://github.com/phoenixframework/phoenix_live_view/pull/2577

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"}
MitchellJeppson

MitchellJeppson

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

Where Next?

Popular in Discussions Top

ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement