Search code examples
erlangelixirphoenix-framework

Correct way of subscribing to Phoenix PubSub with Genserver


Ive been trying to instantiate a genserver process that will subscribe to PubSub in Phoenix framework, these are my files and errors:

config.ex:

config :excalibur, Excalibur.Endpoint,
  pubsub: [
    adapter: Phoenix.PubSub.PG2,
    name: Excalibur.PubSub
  ]

Module using genserver:

defmodule RulesEngine.Router do
  import RulesEngine.Evaluator
  use GenServer
  alias Excalibur.PubSub

  def start_link(_) do
    GenServer.start_link(__MODULE__, name: __MODULE__)
  end

  def init(_) do
    {:ok, {Phoenix.PubSub.subscribe(PubSub, :evaluator)}}
    IO.puts("subscribed")
  end

  # Callbacks

  def handle_info(%{}) do
    IO.puts("received")
  end

  def handle_call({:get, key}, _from, state) do
    {:reply, Map.fetch!(state, key), state}
  end
end

and when i do a iex -S mix this error happens:

** (Mix) Could not start application excalibur: Excalibur.Application.start(:normal, []) returned an error: shutdown: failed to start child: RulesEngine.Router
    ** (EXIT) an exception was raised:
        ** (FunctionClauseError) no function clause matching in Phoenix.PubSub.subscribe/2
            (phoenix_pubsub 1.1.2) lib/phoenix/pubsub.ex:151: Phoenix.PubSub.subscribe(Excalibur.PubSub, :evaluator)
            (excalibur 0.1.0) lib/rules_engine/router.ex:11: RulesEngine.Router.init/1
            (stdlib 3.12.1) gen_server.erl:374: :gen_server.init_it/2
            (stdlib 3.12.1) gen_server.erl:342: :gen_server.init_it/6
            (stdlib 3.12.1) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

So which is the correct way to spin up a Genserver that will subscribe to the PubSub topic ?


Solution

  • As by documentation, Phoenix.PubSub.subscribe/3 has the following spec:

    @spec subscribe(t(), topic(), keyword()) :: :ok | {:error, term()}
    

    where @type topic :: binary(). That said, your init/1 should look like

    def init(_) do
      {:ok, {Phoenix.PubSub.subscribe(PubSub, "evaluator")}}
      |> IO.inspect(label: "subscribed")
    end
    

    Please note I also altered IO.puts/1 to IO.inspect/2 to preserve the returned value.