Add `stream_update` to LiveView Streams

Currently we have stream_insert and stream that can either add new items to a stream or update existing items if they are already present on the client. It would be good to also have stream_update (or alternatively an update_only: true/false option) that would update an existing stream item if it is present on the client side or ignore the item if it is not already present.

The use case is a bunch of users editing items in a tableview of a potentially large data set. If 2 users can see the same item, it would be nice to show the items updating, but if both users are looking at different portions of the dataset, i don’t want items popping into view just because the other user updated them.

I have some time to help to implement the feature, but I would need someone to walk me through the interaction between the LiveView’s concept of the stream on the server side and client side version of the stream first. Alternatively, if there’s a way to handle this use case with the existing streams api, I’m happy to learn.

Hey @lemaster,

this reminded me of Updating a stream element without inserting · Issue #2690 · phoenixframework/phoenix_live_view · GitHub.

I also opened stream_insert update_only aka stream_update by SteffenDE · Pull Request #3573 · phoenixframework/phoenix_live_view · GitHub, where you can see how this could be implemented. There are no tests yet, because I first wanted to see if José thinks that now’s the right time.

If anyone wonders how to do this in the meantime: you can implement this by keeping a MapSet of rendered ids in your assigns and only stream_inserting if the ID is in the set.

3 Likes

Wow, thanks for jumping on this! I think the solution in your PR is much better than maintaining a MapSet on the server. If you are using a limit with the stream, rather than maintaining a MapSet, you would need to maintain a list, and duplicate the logic of the stream yourself. If you mess up there’s a real chance of your stream diverging from your id list.