2

I have an app where 2 people chat. But before they chat, BOTH parties have to press "accept"

on the front end I have a button that calls this function:

function acceptMatch() {
  channel.push("accept_match", {
    "matched_client_email": matchedClientEmail,
  });
}

in my phoenix channel I have this (which is incomplete because I'm not sure what to do):

  def handle_in("accept_match", %{ "matched_client_email" => matched_client_email }, socket) do
    Logger.info "accept_match"
    ...
    ############################### here ####################################
    match_accepted = true # <-- I want to be able to find out if the other person has pressed "accept" as well
    case match_accepted do
      true ->
        ...
        {:noreply, socket}
      _ ->
        {:noreply, socket}
    end
  end

I looked into Agent, but that seems to be tied to a PID, which I wouldn't be able to easily pass around to different sockets. I'm trying to look for a common storage (other than a database) between all connections that I can save to / query. This way if party A presses accept, I can ask the storage "did party B accept?"

=== UPDATE ===

When I try using a named Agent I get this:

iex(11)> myfunc = fn -> %{} end
#Function<20.52032458/0 in :erl_eval.expr/5>
iex(12)> Agent.start_link(myfunc, name: "Cool")
** (CaseClauseError) no case clause matching: {"Cool", []}
    (elixir) lib/gen_server.ex:544: GenServer.do_start/4
iex(12)>

1 Answer 1

3

Actually it seems that you might pass to each new connection in connect in your user_socket this particular PID or name of the Agent and it should solve this case. You only need one Agent, so you can name it, eg.

Agent.start_link(your_func, name: :myagent)

and later get from it

Agent.get(:myagent, your_get_func)

Also you can consider to use Registry, which is from Elixir 1.4 in standard library.

This agent can be in lib and you can just add it in the <app_name>.ex file as a single worker and supervise it with dedicated Supervisor if needed.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.