vfsoraki
In mount/3 callback of a LiveView module, you get session as the second parameter. It works fine, until you use live_redirect/push_redirect to mount another LiveView.
In this case, what is the session passed to second one? The same as original?
Is there a way to change it, without some hacky thing?
I currently did it by using JS to append some signed query string to my url and reload the page, and a plug that reads that signed string and sets appropriate session values, but this feels kind of hacky.
My current use case is user settings page. User changes some settings, then hits save, then goes to another page. User settings is saved in database, but session data is still old, so when new LiveViews are mounted they still see the old setting.
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
wanton7
I might be wrong here because of my limited usage of LiveView. But if session is same as Phoenix session my understanding is that default implementation uses a cookie to save session values. Because LiveView uses Websocket it can’t create a cookie for browser. You likely have to look for alternative to session cookie implementation.
vfsoraki
That’s something I failed to mention, though I don’t think will matter.
I’m not using cookies for session storage. I’m using a custom implementation that uses database for session storage. It’s an implementation for
Plug.Session, so I don’t think it should matter what is my session storage.wanton7
How do match session to database values? I mean you probably still have a cookie that has a session id right? Maybe that cookie isn’t created?
vfsoraki
I do have sessions working fine in normal HTTP requests. It works fine, and my session store also works fine.
LiveViewalso gets the first session correctly.Yes, only session id is saved in a cookie, and session data is stored in database.
I just implemented a store using Plug.Session.Store — Plug v1.20.2. Thats all, nothing tricky or fancy.
Maybe I should’ve reworded my question to something like “How to modify session when using LiveView, without refreshing the page?”
derek-zhou
Presumably the session id will not change because the user changing their settings. You can just load whatever stuff from the database at the mount time of the liveview, right?
vfsoraki
No, session id will not change when it’s modified.
Consider session has
userkey which has the data for current user. I can read this usingdef mount(_, %{"user" => user}, socket)inLiveView, but when I change user in anyway, there is no way to update session, presumably because there is noconn.EDIT: I mean without a full refresh of course, like what
live_redirect/push_redirectdoes.Although one might say that session id is stored in a cookie, and that can be read by
LiveViewand it can also be modified then, but I think there is no way to do it right now.I can reload my user in every
LiveViewlike:But I think there may be a better way.
derek-zhou
What do you mean by a better way? In plain HTTP you read it every time in your plug. In live_redirect your plug did not run so you would need to read it yourself in mount.
vfsoraki
I know, so there is no way to update session before
live_redirect.You either should refresh data in your
mountyourself (using id or whatever) or do a full refresh for plugs to run.wanton7
Googled a bit about this issue and came across this thread that might help you How to manage session state with live view
vfsoraki
Nice solutions were provided at the end of that topic, but they seem kind of hacky as my solution. Thanks!