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!

15 Likes

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?

1 Like

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.

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.

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?

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.

@rio517 Could you give the updated code on the main branch on GitHub a try?

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.

1 Like

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:

1 Like

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:

3 Likes

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

2 Likes

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