What is the best way to check if a GenServer with Registry name exist?


I’d like to know what is best way to know if a GenServer is already running with an existing via Registry name?

I just figure out with start_link and check for :ok or :error, but I don’t know if is the best way to do it


1 Like

Are You looking for this function?



Returns the PID or port identifier with the registered name. Returns nil if the name is not registered.

… should be sufficient for the local registry.


Thanks for answer!

Yeah I already have tried but don’t work as expected, if two process at the same time try to get the name and is not register it will send an empty list for the two, one will get the get :ok from the GenServer and the other :error

Based on the spec:

whereis(atom) :: pid | port | nil

it just accept atoms as names :frowning:

try gproc :slight_smile:

gproc accept string name and support

:gproc.whereis_name({:n, :l, {:go_game, name}})

Thanks, I just have a question, the names generated are unique or duplicated?

When I register the name I use

  defp via_tuple(name) do
    {:via, :gproc, {:n, :l, {:go_game, name}}}

where the key is a unique tuple, with an choosen atom, and a given name

1 Like

Thanks, I’ll take a look ^^/

Typically not an issue as quite frequently GenServers are started like this:

  @spec start() :: {:ok, p} | {:error, a} when p: pid(), a: atom()
  def start(),
    do: GenServer.start_link(__MODULE__, [], name: __MODULE__)


$iex -S mix
iex(1)> Frequency.start
{:ok, #PID<0.226.0>}
iex(2)> Process.whereis Frequency 
iex(3)> Process.whereis :'Elixir.Frequency'
1 Like

Isn’t it an issue with dynamically generated name? Like in a simple_one_for_one case?

All I’m saying is that the parameters weren’t clearly set out in the original question - and at the very least don’t make things more complicated than they need to be.

I’ll try to find the reference but I seem to recall that at least in part of the Erlang community “strings are overrated” is a thing.

Also the global registry can use any term() as a key and has the :global.whereis_name/1 function that returns :undefined when a name is not in use.