thoughtarray
Is it possible to combine LiveView and gen_statem?
I’ve ran into several situations where LiveView and gen_statem being combined would be ideal. The simplest example would be any kind of calculator project. If you make even a simple calculator in LiveView, you’ll eventually want to think about state to keep track of where you are.
I know LiveView is a process running some kind of specialized gen_server. So it’s behavior and state. Similar situation with gen_statem. The only way I can think to combine them is to kick off and link a gen-statem on a LiveView mount, but that uses two processes per client.
Is there a way to do this with one process? Or would I have to write a gen_server from scratch that emulates both LiveView and gen_statem?
Most Liked
RudManusachi
While not answering directly to your question:
I’d say that should be a default way to go. Moreover - that gen_statem would want to live under a different supervisor and be registered under a name that LiveView process for a connection can uniquely identify (for example some user_id).. The reason is - LiveView process might get restarted because of faulty internet connection of a client.. and you don’t want to lose the state of the gen_statem, do you?
The number of processes used is still O(n) where n is the number of active LiveView connections.. (how many concurrent LiveView connections you expect to have simultaneously? 1k? 10k? BEAM by default allows upto 1m processes
)
And thinking of optimizing the number of processes in such case seems like a premature optimization.
Usually it becomes a concern when the logic might dynamically spawn unbound number of processes for some computation.. for example, if you want to implement some sort of a BFS.. and even then there are mechanisms to workaround - such as using process pools which would limit the number of processes used per computation.
thoughtarray
Follow up for others that run across this.
FSM for stateless or very thin-stated things like presentation concerns (as @RudManusachi was talking about), Saša Jurić created a now archived/non-maintained fsm library. His conclusion:
Pure functional FSMs are still my preferred approach (as opposed to gen_statem), but you don’t need this library for that. Regular data structures, such as maps or structs, with pattern matching in multiclauses will serve you just fine.
This is completely compatible with GenServer/LiveView since it’s a process-less approach.
In conclusion, using FSM inside of some process-based thing (GenServer, LiveView, etc) is achievable with Elixir primitives. Not knowing how is a basic skill issue that doesn’t necessarily need to be solved with a library or involved Erlang module.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










