title: Phoenix: How to supervise an Agent?
body: <p>I am trying to use Agent as a store. However, I'm getting issues that the key I'm storing the data under does not exist.</p>
  
  <p>Here is my channel:</p>
  
  <pre><code>  def join("user_pool:" &lt;&gt; email, %{ "app" =&gt; app }, socket) do
      Logger.info "PRODUCTION user_pool email: #{email}, app: #{app}"
      socket = assign(socket, :email, email)
      socket = assign(socket, :app, app)
  
      Agent.start_link(fn -&gt; %{} end, name: :"#{email}") # &lt;---------------- should start link here
      {:ok, socket}
    end
  
  
    def handle_in("approve_match", %{ "matched_client_email" =&gt; matched_client_email }, socket) do
      Logger.info "approve_match"
      current_user_email = socket.assigns[:email]
  
      Agent.update :"#{current_user_email}", fn state -&gt;
        Map.put(state, matched_client_email, true)
      end
  
      match_accepted = Agent.get(:"#{matched_client_email}", &amp;(Map.get(&amp;1, current_user_email))) # &lt;----------- breaks here. says they matched_client_email key does not exist in Agent.
      Logger.info "#{matched_client_email} has approved #{current_user_email}: #{match_accepted}"
      case match_accepted do
        true -&gt;
          VideoChat.Endpoint.broadcast(
            "user_pool:#{matched_client_email}",
            "match_accepted",
            %{ matched_client_email: socket.assigns[:email] }
          )
          {:noreply, socket}
        _ -&gt;
          Logger.info "2nd person has yet to approve"
          {:noreply, socket}
      end
    end
  </code></pre>
  
  <p>I was suggested to create a supervisor for this, but I'm not sure how. I know I have to add something to the commented out line in lib/my_app.exs but I can't find what. I'm also not 100% sure this would fix my issue.</p>
  
  <pre><code>  def start(_type, _args) do
      import Supervisor.Spec
  
      # Define workers and child supervisors to be supervised
      children = [
        # Start the Ecto repository
        supervisor(VideoChat.Repo, []),
        # Start the endpoint when the application starts
        supervisor(VideoChat.Endpoint, []),
        # Start your own worker by calling: VideoChat.Worker.start_link(arg1, arg2, arg3)
        # worker(VideoChat.Worker, [arg1, arg2, arg3]),
      ]
  
      # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
      # for other strategies and supported options
      opts = [strategy: :one_for_one, name: VideoChat.Supervisor]
      Supervisor.start_link(children, opts)
    end
  </code></pre>