Fundamentals of passing data from the Plug connection to the LiveView

Well…assign_new() is still a mystery how it exactly works.
@josevalim was sharp in the 2 recommended alternatives and details. Clear!
But as my example with assign_new() shouldn’t work and was working I kept investigating and discovered that as long as I pass in the router , session: %{"locale" => "some_value"} the call to assign_new() in my Liveview would always work properly, regardless of the value I was passing in the router.
So I tried to push it a little bit further and using plugs injected 2 values in Conn in my pipeline:

  • assign(conn, :test, "Nooo")
  • assign(conn, :locale, "en")
    Then, in the router injected in the session:
    live "/privpol", Priv_polLive, session: %{"locale" => "lol", "test" => "UaU!"}
    Notice the difference in the keys: :atoms in the conn, Strings in the session.
    Finally in my Liveview I have:
def mount(_params, %{"locale" => loc, "test" => test}, socket) do
    socket = assign_new(socket, :locale, fn -> loc end)
    socket = assign_new(socket, :test, fn -> test end)
    IO.inspect loc
    IO.inspect test
    IO.inspect socket.assigns.locale
    IO.inspect socket.assigns.test
    {:ok, socket}
  end

Interestingly everything works and I get the results I expected from conn, not session (I get this in my console):

[debug] Processing with Phoenix.LiveView.Plug.Elixir.LiveappWeb.Priv_polLive/2
Parameters: %{}
Pipelines: [:browser]
"lol"
"UaU!"
"en"
"Nooo"
[info] Sent 200 in 2ms

What puzzles me is that I need to pattern match on the session in my mount/3 in the Liveview to make it work, although the values that assign_new() access as variable are different than the values of the variable!!!
And I’m pattern matching session using string keys and getting the value of atom keys in the conn!!!

Again, …lost! :slight_smile: