petelacey

petelacey

There have been a number of questions on this forum and on SO regarding accessing Conn.assigns in LiveViews that are mounted via the router’s live macro. Here’s a typical one..

Over in this similar question, @chrismccord clearly states

No assigns are shared from the conn to the LiveView mount

My question is why?

Is this a technical issue or a design choice? I’m new to Elixir/Phoenix, so if it’s a technical issue, I would love to hear it. But if it’s a design choice, may I would humbly suggest that it be reconsidered? I ask that because many applications and libraries have fully adopted the notion stated in the Programming Phoenix book that “Your web applications are pipelines of plugs,” and accordingly alter Conn.assigns to communicate up and down the stack. The typical example being current_user. Not having access to these settings is a little clumsy and non-intuitive.

I appreciate that we may not want to wholesale copy Conn.assigns to Socket.assigns (though I don’t know why), but can we have functionality similar to what we have for sessions? That is, something like this:

live "/resource/:resoure_id", ResourceLive, assigns: [:current_user]

Please know that I’m not being critical. I’m loving Phoenix and LiveView, I’m just hoping to understand better and maybe help with a suggestion.

Thanks,
Pete

Showing Posts 1 to 5

LostKobrakai

LostKobrakai

Liveviews are mounted multiple times and only the first time is in the context of your current request. All other times are after the website is rendered and the liveview javascript starts the channels connection for updates. At that time the initial request is fully handled and therefore the assigns are long gone. Only data you put into the liveview session (which gets encoded into the actual html) will be available.

There is however an optimization with assign_new, which allows you skip querying the db for the mentioned first static mount if assigns already holds a value for the same key. Later connections will need to be able to retrieve the same data without assigns being available.

petelacey

petelacey OP

Thank you, @LostKobrakai, that was helpful. I had a pretty good understanding of how liveviews are mounted multiple times, but your response helped clarify things even more. Your explanation of assign_new was especially illuminating.

I’m still unclear, however, as to why the live macro does not offer the onetime opportunity to copy elements from Conn.assigns into the liveview session? In a manner similar to how it currently allows copying from the HTTP session, and how you can manually do so in a controller before a call to live_render.

Anyway, thanks again!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The HTTP session is communicated using cookies, which have size limits since they have to make it all the way to the end client and then get sent back in a follow on request. Conn assigns and socket assigns reside in memory in your servers, and can safely be very large (assuming you don’t run out of ram of course). Most importantly though, there’s no guarantee that the initial page render and the subsequent live render even happen on the same server! If you’re running several servers behind a load balancer, it’s entirely possible that the initial request is handled by server A, and the live render ends up happening through a websocket assigned to server B. Trying to copy arbitrarily large state between them via the client isn’t going to work.

petelacey

petelacey OP

I completely grok all of that, and it perfectly explains why Phoenix wouldn’t want to bulk copy Conn.assisgns to the http session. But that’s not exactly what I’m asking. My question is why Phoenix doesn’t allow us to pluck and item or two out of the assigns and into the socket session? To repeat the example I used earlier, why is the following a bad idea?

live "/resource/:resoure_id", ResourceLive, assigns: [:current_user]

Notice the made up uses of assigns: in that snippet, not the actually supported session:.

In the Elixir Forum post I linked to above, Chris suggests the following as a solution:

Phoenix.Controller.live_render(conn, Mylive, session: %{local: conn.assigns.current_user})

Which makes perfect sense, but isn’t viable if your LV uses live_link/live_redirect. I suppose one could argue that any particular assign might be too large, and that’s why it’s not supported. If so, then that’s cool. In my case, I already have workarounds. Maybe it’s a documentation issue. It just seems a little odd.

Thanks again,
Pete

petelacey

petelacey OP

I’m back to say that @LostKobrakai and @benwilson512 could not have been any clearer or more helpful, but my brain was a bit muddled yesterday. This morning, however, immediately upon waking, the scales fell from my eyes, and everything slid into place. Your responses were so crisp and accurate that I can’t even rephrase them to help future readers. I was just being dense. Anyway, I just wanted to say thanks again. --Pete

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews