Live_redirect not redirecting

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

Hi, try this one:

 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
    socket =
      if is_user_present?(socket.assigns.current_watched_user, Presence.list_presences(@topic)) do
        socket
      else
        IO.inspect("redirect")

        socket
        |> live_redirect(to: Routes.live_path(socket, MsimWeb.Monitor))
      end

    {:noreply, socket}
  end
2 Likes

Your version works. What am I missing here ?

Just a quick guess after glancing over your non-working version and @quatermain version, I would say it has to do with variable scoping rules.

2 Likes

What You miss is… a variable defined in a do … end block will not be accessible to the outside scope. As mentionned by @sfusato.

Don’t do this

... do
  a=1
end
IO.puts a

But do this

a = ... do
  1
end
IO.puts a

As mentionned by @quatermain :slight_smile:

4 Likes

Thank you all, I have learned ! Lesson taught. I am obviously a beginner in functionnal programming, my brain is still wired into procedural. So the non working version was creating a new socket variable, shadowing the original one and not accessible from the outer scope.
:hugs:

1 Like