Global state from a sticky liveview?

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?

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”?

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.

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.

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?

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?

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.