derek-zhou

derek-zhou

Idea: persist liveview state across reconnections

Liveview is nice with one small problem: With mobile, or on shaky network, there will be frequent dropped connections. Yes, Liveview reconnects from the client side automatically, but the state at the server side process is lost. Here is an idea:

  • trap exit in liveview, and when terminate/2 is called, persist the full content of assigns somewhere. (database, flat file, etc), key off an opaque and unique id shared with the client side.
  • at mount, check to see if this unique id can be found in the persistent storage. If so, load the full content into assigns.

There will be some small wrinkles like versioning the saved data, periodic clean up the storage, etc. but it seems like everything should be doable. Any comments?

Most Liked

wanton7

wanton7

Because there might be multiple servers behind one domain and you don’t know which one of those servers WebSocket connection connects that’s why client side state is used. If you have issues with lost state you are not properly storing important state to the client and using that state on reconnect to restore your views. Important state meaning open panels, select element values that are not stored anywhere expect client etc., state that is used to get you to the same view state you where before disconnect but excluding things you can get from your databases. You could example store those open panels to the URL’s query string. You can also use LiveSocket’s params field to send data from client to server on reconnect.

sodapopcan

sodapopcan

Fly did an article that uses client-side session storage for this purpose.

Otherwise, my feeling is that whenever possible, state should always be recreateable via the db and more notably the URL—many neglect the latter. I’m even talking things like tabs that may already have the data loaded but hidden. Just giver ‘er a good ol’ window.history.pushState. But I will now step off my soapbox :slight_smile:

wanton7

wanton7

For game maybe you could create something similar to Microsoft Orleans Virtual actors with processes? I mean like start a GenServer for the game logic that keeps the state and then just pass reference id to that game process to the client and maybe put some info into database that user has access to that game into database or maybe encode it’s id into a JWT token? That game state could then live even in another server. So if you connect to server1 you could just message game state running on server2 where it was originally started. Then that game state could have timeout that makes it die after certain time passes after no keep alive messages. In this case you should only keep minimal game state information in LiveView’s process. Benefit from this is that you could even have multiple LiveViews connecting to same game.

For AI I think that highly depends on what kind of AI data how much. If it’s images you probably store it somewhere anyway and just provide a link.

Where Next?

Popular in Discussions Top

vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
AstonJ
It’s been a while since we’ve had a thread like this, so what better way to kick off the year with :003: What does being an Elixir user ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement