How to define multiple PubSub servers in the supervisor tree?

I am trying to migrate from:
{:phoenix, “~> 1.3.1”}
{:phoenix_pubsub, “~> 1.0”}
{:phoenix, “~> 1.5.6”}
{:phoenix_pubsub, “~> 2.0.0”}
And the PubSub servers has been defined as:

    children = [
      supervisor(Phoenix.PubSub.PG2, [:a, []], restart: :permanent, id: :a),
      supervisor(Phoenix.PubSub.PG2, [:b, []], restart: :permanent, id: :b),

If i define the PubSub servers with the new notation:

    children = [
      {Phoenix.PubSub, name: :a},
      {Phoenix.PubSub, name: :b}

I am getting this output log:

{:error, {:start_spec, {:duplicate_child_name, Phoenix.PubSub.Supervisor}}}

And if i try to define it in this other way:

    children = [
         %{id: :a, start: {Phoenix.PubSub, :start_link, [name: :a]}, type: :supervisor, restart: :permanent},
        %{id: :b, start: {Phoenix.PubSub, :start_link, [name: :b]}, type: :supervisor, restart: :permanent},

I am getting this output log:

{:error, {:shutdown,  {:failed_to_start_child, :pubsub_extension,   {:EXIT,    {:undef,     [       {Phoenix.PubSub, :start_link, [name: :pubsub_extension], []},       {:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', line: 379]},       {:supervisor, :do_start_child, 2, [file: 'supervisor.erl', line: 365]},       {:supervisor, :"-start_children/2-fun-0-", 3,        [file: 'supervisor.erl', line: 349]},       {:supervisor, :children_map, 4, [file: 'supervisor.erl', line: 1157]},       {:supervisor, :init_children, 2, [file: 'supervisor.erl', line: 315]},       {:gen_server, :init_it, 2, [file: 'gen_server.erl', line: 374]},       {:gen_server, :init_it, 6, [file: 'gen_server.erl', line: 342]},       {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}     ]}}}}}

Could you help me ?


Please let me know if you get an answer - could really use this!

I’m having a similar problem. It would be great if someone has a way to fix this and solve this problem.

You should be able to do it this way:

    children = [
      Supervisor.child_spec({Phoenix.PubSub, name: :a}, id: :pubsub_a),
      Supervisor.child_spec({Phoenix.PubSub, name: :b}, id: :pubsub_b)

i.e. you have to provide both: name and ID of the worker.


Thank you! That solution worked for me :slight_smile:

1 Like