The broadcaster node is already correct (only send a new reply)
To fix the receiver node, put these new 3 lines of code. That’s it!
lib/live_comment_web/live/comment_live/show.ex
def preload(list_of_assigns) do
# https://hexdocs.pm/phoenix_live_view/Phoenix.LiveComponent.html#module-preloading-and-update
+ {send_update_assigns, list_of_assigns} = Enum.split_with(list_of_assigns, fn a -> Map.get(a, :prevent_preload) end)
+ if send_update_assigns == [] do
parent_ids = Enum.map(list_of_assigns, & &1.id)
children = Managed.fetch_child_comments(parent_ids)
Enum.map(list_of_assigns, fn assigns ->
Map.put(assigns, :children, Map.get(children, assigns.id, []))
end)
+ else
+ send_update_assigns
+ end
end
lib/live_comment_web/live/comment_live/index.ex
def handle_info({Managed, :new_comment, comment}, socket) do
if comment.parent_id do
- send_update(CommentLive.Show, id: comment.parent_id, children: [comment])
+ send_update(CommentLive.Show, id: comment.parent_id, children: [comment], prevent_preload: true)
{:noreply, socket}
else
{:noreply, assign(socket, comments: [comment])}
end
end


















