_toni

_toni

Multiple LiveView. Share state across them via common socket

Hello :wave:
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

  1. Go to /
    -> Fires up PageLive PID<0.XXX.0>

  2. Go to /second via link at PageLive
    -> Fires up SecondLive
    -> Kills PID<0.YYY.0> from :observer app as far as I can see.

  3. After 3 seconds SecondLive sends itself (0.YYY.0) a new value
    -> assign new value into socket, eg assigns = %{ ..., number = 444}

  4. Go to / via button to land on PageLive again.
    -> Kills PID<0.YYY.0> that was created by SecondLive
    -> Now this creates a new PID<0.ZZZ.0>) that has no access to the previously modified socket with number

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

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

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

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

nshoes

Nope, not possible :slight_smile: 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

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.

Where Next?

Popular in Questions Top

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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement