kostonstyle

kostonstyle

What does name parameter mean

Hi all
I have following module that implement GenServer, the code is from book elixir in action.

defmodule Todo.DatabaseWorker do
  use GenServer

  def start_link(db_folder, worker_id) do
    IO.puts "Starting database worker #{worker_id}"

    GenServer.start_link(
      __MODULE__, db_folder,
      name: via_tuple(worker_id)
    )
  end

 defp via_tuple(worker_id) do
    {:via, Todo.ProcessRegistry, {:database_worker, worker_id}}
  end
end

What I do not understand is the name parameter in the start_link function. What does it mean, why the function via_tuple function return a tuple?

Thanks

Marked As Solved

hubertlepicki

hubertlepicki

Read this, I blogged about namig processes earlier today:

In your example, it’s slightly more complicted scenario, where you are using third party registry Todo.ProcessRegistry. Read this (after you read above):

https://m.alphasights.com/process-registry-in-elixir-a-practical-example-4500ee7c0dcc#.k4zo7xjho

Also Liked

Qqwy

Qqwy

TypeCheck Core Team

When normally (without using the name: option) creating a GenServer, the only way to refer to it, is by its Process ID (PID).

In most simple cases, this is good enough. However, in some cases it is annoying that there isn’t a different alias you can refer to, which means you’ll end up passing a list of PIDs around all over your application.

So instead, it is possible to register a process under a name, which is then stored globally (for the current node!) in Erlang. This means that one can refer to the process by using that name (as well as by its PID). This is often used when you only ever need a single process doing a certain kind. It is very common to have constructs like name: __MODULE__, which means that you only want a single GenServer to run using the code in the current module.

There are a few ways to specify a name other than an atom. @msambarino wrote a great explanation here that goes into more detail.

EDIT: Heh, @hubertlepicki was just a second quicker than I. Great blog post!

kostonstyle

kostonstyle

@hubertlepicki and @Qqwy thanks so much for help. The article process registry in very nice.

Thanks very much guys

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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 Postgres...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement