Erlang's process group module allows adding arbitrary PIDs to a group, 
which can be used for things like 
PubSub: https://www.erlang.org/doc/man/pg.html

Registry's register function currently always registers the current 
process; you cannot register an arbitrary 
process: https://hexdocs.pm/elixir/1.14.5/Registry.html#register/3

However, there are ways to work around this with Registry; see attached 
Livebook markdown for an example.

I propose new functions:

   - Registry.register/4
      - register(pid, registry, key, value)
      - Registry.unregister/3
      - unregister(pid, registry, key)
      - Registry.unregister_match/5
      - unregister_match(pid, registry, key, pattern, guards \\ [])
      - Registry.update_value/4
      - update_value(pid, registry, key, callback)
      
Another (potentially simpler) option might be to have the pid argument be 
at the end and make it default to self().

Thoughts? 😄 

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/8d5ed14e-c477-4295-aa94-d433106b4204n%40googlegroups.com.
# Registry

## Register arbitrary PID

```elixir
defmodule RegistryTest do
  def init() do
    Registry.start_link(
      keys: :duplicate,
      name: Registry.PubSubTest,
      partitions: System.schedulers_online()
    )
  end

  def subscribe_pid(pid) do
    # The third argument of `register` allows any arbitrary data, including a 
pid!
    {:ok, _} = Registry.register(Registry.PubSubTest, "hello", {pid, :other, 
"data"})
  end

  def send_message(message) do
    Registry.dispatch(Registry.PubSubTest, "hello", fn entries ->
      # Then you can just pull the pid out as the "subscriber" pid and send to 
it!
      for {_, {subscriber_pid, _other, _data}} <- entries, do: 
send(subscriber_pid, message)
    end)

    :ok
  end
end

pid1 =
  spawn(fn ->
    receive do
      :hello -> IO.puts("world")
    end
  end)

pid2 =
  spawn(fn ->
    receive do
      :hello -> IO.puts("ocean")
    end
  end)

pid3 =
  spawn(fn ->
    receive do
      :hello -> IO.puts("sky")
    end
  end)

RegistryTest.init()

RegistryTest.subscribe_pid(pid1)
RegistryTest.subscribe_pid(pid2)
RegistryTest.subscribe_pid(pid3)

RegistryTest.send_message(:hello)
```

<!-- livebook:{"output":true} -->

```
world
ocean
sky
```

<!-- livebook:{"output":true} -->

```
:ok
```

Reply via email to