I learnt elixir a few years ago, but then I had to move to Golang and Python a couple of months later since the adoption rate at that time was low. Now, I am learning the language again to apply to companies that work on Elixir.
While learning, I realised that state management in elixir is not as straightforward since it is a functional programming language. While in other languages we can create a singleton class and pass it around everywhere, we can’t do it here.
Thus, in an attempt to display the commonly used mechanisms to manage the state, I have created a GitHub repository containing all my code.
Please go through it in your free time. I would be grateful if you could leave feedback and suggestions. I would want to incorporate all the knowledge gained in the code so that it can serve as a reference point for anyone learning the language.
You might also be interested in this Erlang -- gen_statem
Take notice that many people still view Elixir as a fringe language (regardless of how many things are done with and in support of it, Fly.io and Supabase.com notwithstanding).
GenServer with a non-configurable name (i.e. give it name + ID directly in your app’s supervision tree) and you have a singleton. Don’t try and fixate on how other languages do things, every language needs to manage state otherwise you can’t do anything useful with it.
It looks good overall. I could leave a few small remarks after work. F.ex. I am not sure why you are using
cast for adding and removing users; IMO you would want that to be synchronous for the caller because they might have code relying on a user they just
cast-ed a message to be deleted. If you used
call instead that bug will become impossible.