Phoenix 1.7 streams and sortable datatables

Hi, the new Phoenix streams feature looks really promising, kudos to the Phoenix team! I’d really like to convert my data tables to streams. I’ve read the topics on the forum, Chris McCord’s blog post on and LiveBeats’ source code, but I still don’t see:

  1. How to implement bulk selection with checkboxes (like this: Add bulk actions in Phoenix LiveView - Tutorials and screencasts for Elixir, Phoenix and LiveView).
  2. How to handle sorting and filtering. I’m using PubSub for item creation/update/remove events. When an event is received, I just refetch every items from the context. I know about the at option for stream_insert, but how could I only insert the new items in the right place without duplicating the business logic?

Please tell me if my questions aren’t clear enough, I’d gladly provide more context and/or code snippets.

Hey bro
While chris mccord didn’t provide convenient functions for filtering or handling streams in live view other than delete or update by element, my filtering option clears everything and then refills in the right order by sending broadcast messages

broadcast_refill(search_messages, username)

defp broadcast_refill(filtered_messages, username) do
   broadcast_remove_all(Messages.list_messages(), username)
   broadcast_insert_all(filtered_messages, username)

defp broadcast_remove_all(messages, username),
    do: Enum.each(messages, &broadcast_removed(&1, username))

defp broadcast_insert_all(messages, username),
   do: Enum.each(messages, &broadcast_updated(&1, username))

defp broadcast_removed(message, username),
   do: Messages.broadcast_change({:ok, message}, {:message, :removed}, "#{username}-messages")

defp broadcast_updated(message, username),
   do: Messages.broadcast_change({:ok, message}, {:message, :updated}, "#{username}-messages")

1 Like