rms.mrcs

rms.mrcs

PhonixLiveState - shared state across LiveViews and Controllers

Hi there :waving_hand:

Just dropping by to share PhonixLiveState, a lib I’ve just published.

It’s still VERY RAW, but already in a reasonably usable state.

The idea is to define a state in a process separate from the liveview, which enables some interesting features:

  • Easier to isolate information in a state

  • Auto recovery in case the liveview crashes

  • Share states between liveviews within the cluster

  • Share state between a Phoenix Controller and a LiveView

  • Once connected, you can access your shared state in the template with @live_assigns

  • Yes, the assign is automatically updated when any client makes a change

On top of that, I added a few extra features:

  • Auto shutdown with TTL (configurable) when no clients are connected: prevents zombie processes

  • Read/write permissions with ACL for group/public

Keep in mind that it was originally built to fulfill my own needs on another project, so it might not fit perfectly into your workflow (but I tried my best to keep it use-case agnostic).

Any feedback is more than welcome!

Thanks!!

https://github.com/msramos/phoenix_live_state

Most Liked

Asd

Asd

Good idea, well documented and written code. While I was reading the code, I discovered these questions/issues:

  1. use PhoenixLiveState, :client is essentially a noop. It raises no error, but it also exports no hooks or anything. Why?
  2. If I understood the code correctly, this solution appears to be none from CAP. It’s not consistent or partition tolerant because state server is started under the global name, and when split happens and some new live view mounts in the partition without state server, it will spawn a second server with the same name, which will result in two servers having different state. When split is resolved, one server will be just killed, since server does not implement any callbacks for state handover. It is also not available, because when cluster splits, the views which are in different partition then the state server, will just crash during the GenServer.call to the state server.
  3. When two views do the same action (consider the messages list from the README example), their order will be indefinite and it’s even worse, cause the action which will hit the state server first will be just overwritten by the second one.
  4. Why add ACL to this solution?
  5. It seems that state server spawning is unbalanced, in a sense that the state server always spawns on the node where the first live view with this key mounts

But I like the idea and most of the interface, so I have these suggestions about how to improve the solution:

  1. Decide on CAP. There are AP and CP solutions which I can suggest. First is AP, investigate Delta CRDT algorithms, Merkle Map versions, You can use library Horde (or is it Swarm) which utilizes this algorithm as a reference. If you want to have CP solution, I would suggest to not use the global and implement some persistent hashring to decide on the node where the state server spawns (no matter where the liveview was first mounted).

  2. I don’t see the use case for ACL, but even if there is one, it’s not that hard to implement it on top of the key-valye sync. Doing it inside the library just blurs the scope and, personally for me, looks odd.

  3. Make most logs debug or at least add some metadata so that users of the library could filter these messages out

  4. I believe that every distributed algorithm must have a good doc with implementation details and choice it makes in order for users to understand the possible drawbacks and failure scenarios.

Where Next?

Popular in Announcing Top

ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
rodloboz
I’ve started working on a new library to run SQL queries and do basic business intelligence. Think “Blazer for Elixir.” Currently it fe...
New
zachdaniel
:smiling_face_with_sunglasses: New package usage_rules released! Just place a usage-rules.md file in your package and users can sync it t...
#ai
New
halostatue
Enviable is a small collection of functions to make working with environment variables easier when configuring Elixir projects. It is des...
New
fteschke
Announcing: playwright_ex A simple Elixir client for the Playwright node.js server. Automate browsers like Chromium, Firefox, Safari an...
New
fuelen
Hi all! Confispex is a tool which allows defining specs for runtime configuration, cast values according to specified types and inspect ...
New
Mati365
Phoenix has great tooling overall, but one thing that’s still lacking is a solid, plug-and-play integration with a modern WYSIWYG editor....
New
jstimps
I’ve started development on an Ecto Adapter for FoundationDB: GitHub - ecto_foundationdb. FoundationDB is a distributed database with AC...
New
rkallos
Peep is a new TelemetryMetrics reporter that supports both StatsD (and Dogstatsd) and Prometheus. While load testing a new Websocket-bas...
New
waseigo
I saw this LinkedIn post: *Can your programming language do this? This is a macro in Clojure called `dotrace`. When you surround a pie...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement