wmnnd
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:
https://github.com/pentacent/phoenix_live_session
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!
Trending in Announcing
Other Trending Topics
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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 usePhoenixLiveSession.put_sessionas 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
Hey @rio517 thanks for checking out PhoenixLiveSession!
I’ve given your question some thought and maybe it would be convenient to have
put_sessionaccept the session ID instead of a socket as well.Would the following additional APIs make LiveSession work for your use case?
session_id = get_session_id(session)- to be called in the LiveView’s mount callback.put_session(session_id, key, value)- which you can use in your components.rio517
That would be great! It might also make sense to make the the pub_sub configuration in the endpoint configuration?
Thanks for considering my use case.
wmnnd
I’ll see if I can’t add this over the holidays
What do you mean about the pub_sub config in the endpoint configuration?
rio517
Thanks! That would be awesome.
Re: the
pub_subbit, I was referring to the@session_optionsconfig inlib/my_app_web/endpoint.ex. Since I wouldn’t be subscribing to anything, that subscription might not be required. I got an error when I tried to leave it out.wmnnd
@rio517 Could you give the updated code on the
mainbranch on GitHub a try?https://github.com/pentacent/phoenix_live_session
You can use the
sessionparameter frommount/3and pass it to your components. Then useput_session/3with that data structure instead of the LiveView socket.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.Socketstruct.Thanks to @johns10davenport for reporting the problem and @josevalim for pointing out the solution
You can get it now on Hex:
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 uploadsfeature, 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 toPubSubmessagingmounteventI think that this solution should be standardized into Phoenix LiveView
wmnnd
Thanks, I’m glad you’re finding it useful