Get State from a GenServer process in Elixir


1 min read

TIL that I have been doing this completely wrong.

I have been using this custom implementation:

defmodule MyModule do
 use GenServer

  def report(server) do, :report)

  # More functions

  @impl true
  def handle_call(:report, _from, state) do
    # No modifications. Return the current state
    {:reply, state, state}

It is all good and it works. However, it turns out this is all I needed to do

{:ok, pid} = MyModule.start_link([])
# ... some more operations...

Well, it does say right there in the get_state docs that the whole purpose is to help the users not to reimplement it.

Now I know and so do you ๐Ÿ˜€

More resources: