wmnnd
PhoenixLiveSession: Plug sessions with LiveView integration + PubSub support
Hey folks,
I’ve just published a little library called PhoenixLiveSession.
What is it?
PhoenixLiveSession is a drop-in replacement for the default Plug.Session stores, comes with LiveView integration and Phoenix.PubSub support.
Why did I create it?
My motivation for this was a client project in which I wanted to implement a shopping cart using LiveView.
I wanted to store the shopping cart data in the user session, but so far, doing this with LiveView required some hacky JavaScript trickery and required additional endpoints.
What does it do?
With PhoenixLiveSession you can use put_session/3 on your LiveView sockets and receive session updates via Phoenix.PubSub.
Where can I find it?
You can find the library on Hex:
Contributions on GitHub are welcome:
Also check out the blog article I’ve written about the library with code examples and further insights:
I’m curious to hear what you think!
Most Liked
wmnnd
Here’s another small update to Phoenix LiveSession, version 0.1.3.
It fixes an exception caused in some setups in which the session had not been properly initialized prior to calling maybe_subscribe/1.
You can get it now on Hex:
crockwave
Brilliant solution
This library adds value to the new LiveView uploads feature, because it provides easy to understand support for allowing LiveView uploads based features such as changing a user avatar or maintaining a shopping cart, to be able to
- Directly mutate the session
- Be immediately available to all other LiveViews via
PubSubmessaging - Provide a mutated session immediately available for a LiveView
mountevent
I think that this solution should be standardized into Phoenix LiveView
rio517
This is pretty neat. I was just dealing with some issues close to this and wishing I could set something into the session from my LiveView.
My use case is a bit different than yours. and would love to get your perspective. Disclaimer: I’m only a 4 weeks into my first phoenix app.
I have a “current project” selector that I’m pushing into the session, similar to how one might handle a current_user. My selector is loaded in a live_component menu of every page, thus any mounted liveview might trigger the session insertion. It seems like live_components wouldn’t quite be supported by your library, without explicitly passing the session to them somehow. Did I read that right? I also wonder if I could use PhoenixLiveSession.put_session as a stand-alone and redirect to a new liveview rather than subscribing to PubSub.
An easy way to handle those usecases would be nice, but sounds like that is out of the scope of what you want to do with this?
wmnnd
wmnnd
Here’s a small update to Phoenix LiveSession, version 0.1.2.
It fixes a reliance on an undocumented, now deprecated attribute of the Phoenix.LiveView.Socket struct.
Thanks to @johns10davenport for reporting the problem and @josevalim for pointing out the solution 
You can get it now on Hex:







