absowoot

absowoot

I’ve read numerous threads on the forum about maintaining a global state and it appears that the consensus is to utilize ETS ala Cachex, Phoenix Live Session, etc. Before I head down that road, I wanted to see if anyone has tried handling global state with a sticky liveview.

I think I could use on_mount to send a message to a sticky liveview, which then responds back with data, but I’m unsure how to obtain the sticky liveview PID to send the request to once it has already been loaded. Perhaps this could be stored in a session (server or client-side) or is there a a Liveview function to see all liveviews for a session?

Are there security concerns that would need to be considered?

Showing Posts 1 to 6

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Perhaps you’re using this in a way that is different from what I understand but I don’t see how this is makes sense. If you have multiple connected liveviews, which is the source of truth? What if that source goes away and is never seen again?

What sort of state are you working with and what do you mean by “global”?

absowoot

absowoot OP

Sorry, I should have clarified! By global state, I mean per user ie. a shopping cart for a user that may or may not be logged in.

For context, the app I’m working on is powered by third parties – user, carts, and products all come through API calls. The cart lives in a sticky liveview and its data is also used on a few pages, so the API is called multiple times while navigating the website. I was looking to have the cart in my sticky liveview be the source of truth for these pages instead of hitting the API since the data has already been fetched.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Sure but the thing I want to emphasize is that a live view is not 1:1 to a user, and liveviews go away when people aren’t looking at them sticky or not. “Sticky” just allows you to have state preserved between page redirects, it doesn’t keep state around when you close your browser. If you have 2 tabs open you have two different sticky liveviews.

absowoot

absowoot OP

The cart ID is stored in a cookie and is updated through a broadcast so I think that would cover those scenarios.

Based on your responses, feels like this isn’t a good solution and I would be better off using an alternative method?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The cart ID or the cart contents? It sounds like you’re saying that the cart contents would be in the liveview, or is the cart contents in the cookie too?

absowoot

absowoot OP

The cart ID lives in a cookie, which is then used to fetch the cart contents from the API. When products are added/removed, a new cart contents map is broadcasted using the cart ID to browsers that are subscribed to that ID.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews