wmnnd

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

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

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

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

@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.

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

You can get it now on Hex:

Where Next?

Popular in Libraries Top

RobertDober
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
239 11851 134
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
pkrawat1
Hey guyz We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it. Have...
New
nikokozak
Hello all, I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
dbern
I’m excited to announce that TaxJar has developed and open-sourced DateTimeParser. We developed it because we found a need to parse user ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
Qqwy
While not as prevalent as in imperative languages, arrays (collections with efficient random element access) are still very useful in Eli...
New
bryanjos
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New
Hal9000
Here is my first stab at this. README pasted below. https://github.com/Hal9000/elixir_random Comments and critiques are welcome. Th...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Sub Categories:

We're in Beta

About us Mission Statement