thoughtarray

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

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 :slightly_smiling_face:)

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

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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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 54120 245
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement