Failed to start child: Phoenix.PubSub.PG2

I am trying to have two EndPoint in the phoenix app, but I get this error when starting the server:

** (Mix) Could not start application myapp: MayApp.start(:normal, []) returned an error: shutdown: failed to start child: MayApp.Schools.Endpoint
    ** (EXIT) shutdown: failed to start child: Phoenix.PubSub.PG2
        ** (EXIT) already started: #PID<0.393.0>

Here is the children supervisors,

children = [
      # Start the endpoint when the application starts
      supervisor(MayApp.Endpoint, []),
      
      # Start the endpoint for schools
      supervisor(MayApp.Schools.Endpoint, []),

      # Start the Ecto repository
      supervisor(MayApp.Repo, []),
      # Here you could define other workers and supervisors as children
      # worker(MayApp.SomeWorker, [], name: SomeWorker),
    ]

MayApp.Schools.Endpoint is a copy of MayApp.Endpoint only socket "/socket" is pointing to another module.

Any idea?

2 Likes

Your endpoint starts the pubsub server based on the endpoint configuration. So you need to configure only one of those endpoints to start the pubsub server, and the other one can simply be configured to use the registered pubsub name of the other. For example:

config :my_app, MyApp.Enpdpoint1,
  pubsub: [name: MyApp.PubSub, adapter: Phoenix.PubSub.PG2]

...
config :my_app, MyApp.Enpdpoint2,
  pubsub: [name: MyApp.PubSub]
2 Likes

Thanks

1 Like

For anyone in the future searching for solutions, before you change your endpoints. After experiencing the same error today, it took a surprisingly easier solution.
All I did was →

mix deps.clean --all
mix deps.get

Then the application was able to start without any issues.