_toni
Multiple LiveView. Share state across them via common socket
Hello 
I followed some interesting thread , thread2 & others regarding state-management with non-trivial UI situations (eg a forum or a shopping ecommerce with an ubiquituous cart) and a likely candidate to good practice is using PubSub or similar channel to store state.
My final use-case is very similar to a shopping cart, but I here isolate the problem to see if what I’m trying to do is possible and how.
# Router
live "/", PageLive
live "/second", SecondLive
# both at root.html.leex & live.html.leex
<%= inner_content %>
--- PageLive
# at "render" function ( also tried live_patch)
<%= live_redirect "SecondPage", to: Routes.live_path(@socket, LivesocketsWeb.SecondLive) %>
---- SecondLive
# at "render" ( also tried live_patch)
<%= live_redirect "Go to FirstPage", to: Routes.live_path(@socket, LivesocketsWeb.PageLive) %>
# at "handle_info"
# --> some logic to write to socket
socket = assign(socket, number: Enum.random(1..5000))
Then I’m interacting this way with the UI
-
Go to
/
-> Fires upPageLivePID<0.XXX.0> -
Go to
/secondvia link atPageLive
-> Fires upSecondLive
-> Kills PID<0.YYY.0> from:observerapp as far as I can see. -
After 3 seconds
SecondLivesends itself (0.YYY.0) a new value
-> assign new value into socket, egassigns = %{ ..., number = 444} -
Go to
/via button to land onPageLiveagain.
-> Kills PID<0.YYY.0> that was created bySecondLive
-> Now this creates a new PID<0.ZZZ.0>) that has no access to the previously modified socket withnumber
Question: Other than using PubSub is there a way to keep the same socket (ie its assigned data) alive during route transitions to other LiveViews?
This is very much a gap in my understanding of the socket lifecycle when routing around liveviews, so any help highly appreciated, thank you!
Repo here https://github.com/git-toni/nested-lvs
Most Liked
Ninigi
Hey man
I just wrote a very lengthy reply to another thread rambling about how we are using LV in our company right now…
I think I should have rambled about it here, but instead please read this (skip ahead if you are not interested)
malloryerik
Seems like Phoenix LiveSession might be similar to what you guys are thinking? Or maybe could provide inspiration? It uses GenServer, PubSub and an ETS table. The canonical use case is a shopping cart.
@wmnnd, the author, introduced it on this forum here.
nshoes
Each LiveView has it’s own process, and the process gets killed when navigating away from it, like your example shows. The only way to keep some state between them is to persist it in another process.
nshoes
Nope, not possible
You can have a single LiveView pass down assigns to LiveComponents, but that’s it. I think we are starting to go in circles here, maybe let’s shift focus. What are you trying to accomplish?
Ninigi
I guess I should clarify that most apps probably would not need to use this pattern - you can create perfect apps with just one liveview, maybe using live components… We just started using this pattern because we had complex logic on different pages, but wanted to be able to drop in all those parts on different pages - mix and match if you want.







