wmnnd

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!

Showing Posts 1 to 10

rio517

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 OP

Hey @rio517 thanks for checking out PhoenixLiveSession!

I’ve given your question some thought and maybe it would be convenient to have put_session accept 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

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

wmnnd OP

I’ll see if I can’t add this over the holidays :slight_smile: What do you mean about the pub_sub config in the endpoint configuration?

rio517

rio517

Thanks! That would be awesome.

Re: the pub_sub bit, I was referring to the @session_options config in lib/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

wmnnd OP

@rio517 Could you give the updated code on the main branch on GitHub a try?
https://github.com/pentacent/phoenix_live_session

You can use the session parameter from mount/3 and pass it to your components. Then use put_session/3 with that data structure instead of the LiveView socket.

wmnnd

wmnnd OP

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 :slight_smile:

You can get it now on Hex:

wmnnd

wmnnd OP

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

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 PubSub messaging
  • Provide a mutated session immediately available for a LiveView mount event

I think that this solution should be standardized into Phoenix LiveView

wmnnd

wmnnd OP

Thanks, I’m glad you’re finding it useful :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Announcing Top

restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 12366 136
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
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
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
kip
I’ll shortly be launching Text, a nascent text analysis library. Current functionality In this early version (not ready for prime time) ...
New
kip
Following on from my CLDR lbraries I started work on Unicode transforms. But like everything related to CLDR there is a lot of yak-shavin...
New

Other Trending Topics Top

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
KristerV
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews