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

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews