How to pass data from one liveView to another liveview through live_redirect?

I have 2 liveviews, A and B. A will live_redirect to B sometimes, can I initialize B with some of the existing assigns in A? Right now I figure out a way to pass some data via the client side:

  • before redirect, push some data to the client side and stash in localStorage
  • after the redirect client push back the stashed data to the new liveView

However, it only works for a small amount of data. If I want to push megabytes of data, is there a way to do it completely on the server side?

1 Like

In this case, your best option is to store the data somewhere and have the new LiveView fetch it. I have discussed with Chris the ability to start LiveViews from the server, which would allow this hand-off to happen more trivially.

1 Like

I see. So now the next LiveView spin up after the previous one quit, so there is no chance to do the hand-off directly, right?

I am going to continue to use the client side localStorage to stash smallish data because it is the easiest. For bigger data structure I can stash them in a GenServer with an auto-generated token, then stash the token in the client side localStorage.

The alternative is to make A and B the same liveView and use live_patch instead of live_redirect. The down side is it is not always possible and will make the LiveView module complex.

A GenServer should be enough because we are going to use the same WebSocket connection which is in the same node. But keep in mind that if the user disconnects, they may reach another node altogether. So consider storing the data somewhere persistent too, as you would if you were not using LiveView in the first place.

2 Likes