ebart

ebart

How to use Registry with Genservers to get or set data in children processes?

I’ve read interesting topic where I found practical example of how to use new Registry. Based on mentioned topic I’ve created Supervisors and Genservers, but I’m not sure if this implementation is correct for my needs or if I’m missing something. I want to create Room by room id than under it spawn Groups and many Users, Room should store all users and groups ids to fast check if user or group exists. Also I want to let user join and leave at once many groups or get all groups that user belongs to. In code below I got stuck in all functions with require IEx;IEx.pry, e.g. :get_user_groups how can I get user groups from children processes? Can I use Registry to subscribe users to their groups or in current implementation can I just extract this data by e.g. Registry.lookup?

Supervisors:

defmodule MyApp.Room.SystemsSupervisor do
  use Supervisor

  def start_link() do
    IO.puts "Starting MyApp.Room.SystemsSupervisor"

    Supervisor.start_link(
      __MODULE__,
      nil
    )
  end

  def init(_) do
    children = [
      supervisor(Registry, [:unique, Registry.ProcessRegistry], id: :proc),
      supervisor(MyApp.Room.RoomSupervisor, [])
    ]

    supervise(children, strategy: :rest_for_one)
  end
end

defmodule MyApp.Room.RoomSupervisor do
  use Supervisor

  def start_link() do
    IO.puts "Starting MyApp.Room.RoomSupervisor"

    Supervisor.start_link(
      __MODULE__,
      nil,
      name: :rooms_supervisor
    )
  end

  def start_child(room_id, user) do
    Supervisor.start_child(
      :rooms_supervisor,
      [room_id, user]
    )
  end

  def init(_) do
    children = [
      supervisor(MyApp.Room.SingleRoomSupervisor, []),
    ]

    supervise(children, strategy: :simple_one_for_one)
  end
end

defmodule MyApp.Room.SingleRoomSupervisor do
  use Supervisor

  def start_link(room_id, user) do
    IO.puts "Starting SingleRoomSupervisor"

    Supervisor.start_link(
      __MODULE__,
      {room_id, user},
      name: via_tuple(room_id)
    )
  end

  defp via_tuple(room_id) do
    {:via, Registry, {Registry.ProcessRegistry, {:room_sup, room_id}}}
  end

  def init({room_id, user}) do
    children = [
      supervisor(MyApp.Room.UserSupervisor, [room_id]),
      supervisor(MyApp.Room.GroupSupervisor, [room_id]),
      worker(MyApp.Room.Room, [room_id, user]),
    ]

    supervise(children, strategy: :one_for_all)
  end
end

defmodule MyApp.Room.GroupSupervisor do
  use Supervisor

  def start_link(room_id) do
    IO.puts "Starting MyApp.Room.GroupSupervisor"

    Supervisor.start_link(
      __MODULE__,
      nil,
      name: via_tuple(room_id)
    )
  end

  defp via_tuple(room_id) do
    {:via, Registry, {Registry.ProcessRegistry, {:group_sup, room_id}}}
  end

  def start_child(room_id, group_id, user_id) do
    Supervisor.start_child(
      via_tuple(room_id), #user_id
      [group_id, user_id]
    )
  end

  def init(_) do
    children = [
      worker(MyApp.Room.Group, [])
    ]

    supervise(children, strategy: :simple_one_for_one)
  end
end

defmodule MyApp.Room.UserSupervisor do
  use Supervisor

  def start_link(room_id) do
    IO.puts "Starting MyApp.Room.UserSupervisor"

    Supervisor.start_link(
      __MODULE__,
      nil,
      name: via_tuple(room_id)
    )
  end

  defp via_tuple(room_id) do
    {:via, Registry, {Registry.ProcessRegistry, {:user_sup, room_id}}}
  end

  def start_child(room_id, user) do
    Supervisor.start_child(
      via_tuple(room_id),
      [user.id, user.username]
    )
  end

  def init(_) do
    children = [
      worker(MyApp.Room.User, [])
    ]

    supervise(children, strategy: :simple_one_for_one)
  end
end

Genservers:

defmodule MyApp.Room.Room do
  use GenServer

  defstruct groups: [], users: [], room_id: :none

  # Client API

  def start_link(room_id, user) do
    IO.puts "Starting Room for #{room_id}"

    GenServer.start_link(
      __MODULE__,
      {room_id, user},
      name: via_tuple(room_id)
    )
  end

  defp via_tuple(room_id) do
    {:via, Registry, {Registry.ProcessRegistry, {:room, room_id}}}
  end

  def new(room_id, user) do
    # r = Registry.lookup(Registry.ProcessRegistry, {:room, room_id})
    # g = GenServer.whereis(via_tuple(room_id))
    # IO.inspect {g, "vs", r}
    case GenServer.whereis(via_tuple(room_id)) do
      nil ->
        MyApp.Room.RoomSupervisor.start_child(room_id, user)
      room_pid ->
        send(room_pid, {:real_init, room_id, user})
    end
  end

  def get_user_groups(room_id, user_id) do
    GenServer.call(via_tuple(room_id), {:get_user_groups, user_id}, 50 * 10000)
  end

  def user_joined_groups(room_id, user_id, groups) do
    GenServer.call(via_tuple(room_id), {:user_joined_groups, user_id, groups}, 50 * 10000)
  end

  def user_left_groups(room_id, user_id, groups) do
    GenServer.call(via_tuple(room_id), {:user_left_groups, user_id, groups}, 50 * 10000)
  end
  
  # Server Callbacks

  def init({room_id, user}) do
    send(self(), {:real_init, room_id, user})

    {:ok, %__MODULE__{room_id: room_id}}
  end

  def handle_call({:get_user_groups, user_id}, _from, state) do
    require IEx;IEx.pry
    {:noreply, state}
  end
  
  def handle_call({:user_joined_groups, user_id, groups}, _from, state) do
    require IEx;IEx.pry
    {:noreply, state}
  end

  def handle_call({:user_left_groups, user_id, groups}, _from, state) do
    require IEx;IEx.pry
    {:noreply, state}
  end

  def handle_info({:real_init, room_id, user}, state) do
    areas = prepare_areas(room_id, user)
    {:ok, _user} = MyApp.Room.UserSupervisor.start_child(room_id, user)

    {:noreply, %__MODULE__{state | areas: state.areas ++ areas, users: state.users ++ [user.id]}}
  end

  # Helper Functions

  defp prepare_areas(room_id, user) do
    for group <- user.groups do
      MyApp.Room.Group.new(room_id, group, user.id)
      group.id
    end
  end
end

defmodule MyApp.Room.Group do
  use GenServer

  defstruct users: [], group_id: :none

  # Client API

  def start_link(group_id, user_id) do
    IO.puts "Starting group: #{group_id}"

    GenServer.start_link(
      __MODULE__,
      [group_id, user_id],
      name: via_tuple(group_id)
    )
  end

  defp via_tuple(group_id) do
    {:via, Registry, {Registry.ProcessRegistry, {:group, group_id}}}
  end

  def new(room_id, group_id, user_id) do
    case GenServer.whereis(via_tuple(group_id)) do
      nil ->
        IO.inspect {"creating group #{group_id} with user: #{user_id}"}
        MyApp.Room.GroupSupervisor.start_child(room_id, group_id, user_id)
      group_pid ->
        IO.inspect {"updating group #{group_id} with user: #{user_id}"}
        send(group_pid, {:add_user, user_id})
    end
  end

  # Server Callbacks

  def init([group_id, user_id]) do
    {:ok, %__MODULE__{group_id: group_id, users: [user_id]}}
  end

  def handle_info({:add_user, user_id}, state) do
    {:noreply, %__MODULE__{state | users: state.users ++ [user_id]}}
  end
end


defmodule MyApp.Room.User do
  use GenServer

  defstruct username: nil, id: :none, metas: []

  # Client API

  def start_link(user_id, username) do
    IO.puts "Starting user: #{user_id}"

    GenServer.start_link(
      __MODULE__,
      [user_id, username],
      name: via_tuple(user_id)
    )
  end

  defp via_tuple(user_id) do
    {:via, Registry, {Registry.ProcessRegistry, {:user, user_id}}}
  end

  # Server Callbacks

  def init([user_id, username]) do
    {:ok, %__MODULE__{id: user_id, username: username}}
  end

end

First Post!

kokolegorille

kokolegorille

Hello @ebart

It’s not quite clear (to me) what You really want to achieve… Can You elaborate?

Why do You want to spawn a group process?
What is the link between User and Roles? I guess User can be member of group.

I guess if I wanted to know get_user_groups, I would not query the room, but the user.

Also if I wanted to send/receive info from a group of genserver, I would not ask Registry, but I would query the supervisor. (Supervisor.which_children)

Greetings

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement