ebart

ebart

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

Showing Posts 1 to 1

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews