Start genserver automatically?

I suppose by connect you mean the PID of the genserver?

If you are planning on having only this genserver running, you can start it with name registration:

def start_link(opts \\ []) do
    GenServer.start_link(__MODULE__, [], name: MessageServer)
end

This doesn’t have to be the module name, you can use any atom, but you have to ensure that it is unique in your system.

In this way you can address the genserver from everywhere by the name you defined:

GenServer.call(MessageServer, :hello)
2 Likes