phoenixframework / phoenix_pubsub

Distributed PubSub and Presence platform for the Phoenix Framework
http://www.phoenixframework.org/
MIT License
653 stars 124 forks source link

unknown registry: XXX.PubSub #144

Closed michaelst closed 4 years ago

michaelst commented 4 years ago

With the update to v2 I am starting to see this error. It appears that I have everything configured correctly as it is still working, but just getting this error occasionally.


lib/registry.ex in Registry.info!/1 at line 1239
lib/registry.ex in Registry.register/3 at line 920
lib/phoenix/pubsub.ex in Phoenix.PubSub.subscribe/3 at line 117
lib/phoenix/channel/server.ex in Phoenix.Channel.Server.init_join/3 at line 401
lib/phoenix/channel/server.ex in Phoenix.Channel.Server.channel_join/4 at line 375
lib/phoenix/channel/server.ex in Phoenix.Channel.Server.handle_info/2 at line 299
gen_server.erl in :gen_server.try_dispatch/4 at line 637
gen_server.erl in :gen_server.handle_msg/6 at line 711
proc_lib.erl in :proc_lib.init_p_do_apply/3 at line 249
chrismccord commented 4 years ago

What is the order of your supervision tree in application.ex?

michaelst commented 4 years ago

Ignore the second piece of the tuple on each, we do filtering on env.

Does pubsub need to be before endpoint?


    [
      {Repo, :all},
      {Endpoint, :all},
      ...
      {{Phoenix.PubSub,
        [
          name: ZB.PubSub,
          adapter: Phoenix.PubSub.Redis,
          url: Application.get_env(:redix, :url),
          node_name: System.get_env("HOSTNAME", "local")
        ]}, :all}
    ] 
chrismccord commented 4 years ago

Yes, you need to start PubSub before your endpoint, as you can race the pubsub server by servicing requests/channels that use pubsub before it's up :)

baxterjfinch commented 4 years ago

I am having a compilation error similar to this and my PubSub is started before Endpoint.

I have three apps under lib. PubSub is added to the application.ex under the main app called metadata.

children = [
      Metadata.Repo,
      {Phoenix.PubSub, name: :metadata_pubsub},
      MetadataWeb.Endpoint,
      MetadataWeb.Telemetry
    ]

I then add this to a controller under the metadata_web app which inserts something in my database

PubSub.broadcast :metadata_pubsub, "item", {:item_created, %{test: item}}

I have a route in metadata_web that points to a live_dashboard app:

scope "/live", MetadataWeb do
    pipe_through :browser
    live_dashboard "/", metrics: MetadataWeb.Telemetry
  end

and under lib/live_dashboard/live/item_live.ex I do the following:

defmodule Phoenix.LiveDashboard.ItemLive do
   ... stuff ...
     alias Phoenix.PubSub
     PubSub.subscribe :mythical_pubsub, "dgood"

but then I get this error:

== Compilation error in file lib/live_dashboard/live/item_live.ex ==
** (ArgumentError) unknown registry: :metadata_pubsub
    (elixir 1.10.2) lib/registry.ex:1239: Registry.info!/1
    (elixir 1.10.2) lib/registry.ex:920: Registry.register/3
    (phoenix_pubsub 2.0.0) lib/phoenix/pubsub.ex:117: Phoenix.PubSub.subscribe/3
    lib/live_dashboard/live/item_live.ex:12: (module)
    (stdlib 3.11.2) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir 1.10.2) lib/kernel/parallel_compiler.ex:304: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

I'm new to phoenix and elixir in general so let me know if this project layout doesn't make sense or if I'm doing something obviously incorrect.

Thank you!