owaisqayum

owaisqayum

How to give GenServer process a name?

While starting a genserver process, I want to give it a name. As you can see in the following example pid is right now acting as a name for the genserver. How can i register table as the name of GenServer.
{:ok, pid} = start_base(). So if i pass table is start_base and then into BaseServer.startlink(), how can i modify my GenServer.start_link(), so that the table acts as the name of GenServer.

Here is my code:

def start_base() do
    BaseServer.start_link()
end

def creating_tables(tables_list) do
    Enum.map(tables_list, fn table ->
      {:ok, pid} = start_base()
      IO.puts("Pid of #{table} is #{inspect(pid)} ")
      BaseServer.create_table(pid, table)
    end)
  end

My Genserver:

# client
  def start_link do
    GenServer.start_link(__MODULE__, [])
  end

  def create_table(pid, table) do
    GenServer.call(pid, {:create_table, table})
  end

right now am passing the pid but how can I register it with the table name.

Thanks

Most Liked

Laetitia

Laetitia

The way to give dynamically a name to a genserver is using the Registry.

def start_link(table) do
    GenServer.start_link(__MODULE__, [], name: {:via, Registry, {:your_registry_name, table}})
  end

You can read more info in:

https://hexdocs.pm/elixir/GenServer.html#module-name-registration

:your_registry_name may be started under your application supervisor.

Laetitia

Laetitia

Yes. The standard way is the one you propose. But it generates dynamically atoms. If the goal is to start a lot of genserver on the fly, it could become problematic.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement