Hi there,
In a live-view presence_diff handler, I have two versions of code, one works, (the live_redirect happens) but not the other one. I can’t figure out why.
The NON working version
def handle_info(%{event: "presence_diff"}, socket) do
socket = assign(socket, users: Presence.list_presences(@topic))
# if user is gone, we redirect to stop watching him
unless is_user_present?(socket.assigns.current_watched_user, Presence.list_presences(@topic)) do
IO.inspect("redirect")
socket =
socket
|> live_redirect(to: Routes.live_path(socket, MsimWeb.Monitor))
end
{:noreply, socket}
end
The working version
def handle_info(%{event: "presence_diff"}, socket) do
# if user is gone, we redirect to stop watching him
unless is_user_present?(socket.assigns.current_watched_user, Presence.list_presences(@topic)) do
IO.inspect("redirect")
{:noreply,
socket
|> assign(users: Presence.list_presences(@topic))
|> live_redirect(to: Routes.live_path(socket, MsimWeb.Monitor))}
else
IO.inspect("do not redirect")
{:noreply,
assign(socket,
users: Presence.list_presences(@topic)
)}
end
end
Note : in both cases, I see the “redirect” in my console.
I’d be glad if someone can enlight me.
Thank you