Pubsub in a phoenix app under umbrella

When creating a new project with

mix proj --umbrella

the phoenix_pubsub dependency is placed in the mix.exs of the proj app and not under the proj_web app.

but that seems against app isolation. since the proj app, in the idea, should have no dependency on phoenix nor any web consideration.

more over when manually placing the phoenix pub_sub in the proj_web mix.exs and adjusting the config of the umbrella config, it does not work with message

unknown registry: ProjWeb.PubSub

how to make i work with phoenix_pubsub under the web app ?

PubSub, even though used by the channels, is not a web concern. It is to send (publish) messages to a couple of subscribers, in a way that the publisher does not need to know the subscribers, while also the subscribers do not need to know the publisher(s) in advance.

It can be used for a lot more things than just “web”.

1 Like

All right, but having both mix apps proj and proj_web under umbrella, what if I wanted to place the phoenix pub sub dependency (which is needed by proj_web --the phoenix app-- ) under the proj_web, because I don’t need pubsub in the proj app and pubsub is needed by the phoenix app ?

That should be no problem for as long as you make sure pubsub is started before the endpoint in your supervision tree.

How to make that afterwards ?

there are two scenarios :

1 using mix proj --umbrella

it works out of the box but if I move the dependency to the proj_web mix.exs it does not work
anymore with the error message previously cited

2 creating a bare mix umbrella project and then mix --noecto --live

when the dependency is moved, the config file at the top of the umbrella project must be modified, that’s ok, but
even in scenario 1 I see no where PubSub is started in a supervision tree

You need to move both the dependency and where pubsub is started in the supervision tree (application.ex) and rename all the places where the module name is mentioned.

even in a bare phoenix project Proj/ProjWeb not under an umbrella there is no place where the Proj.PubSub is explicitly started

If I try to put Proj.PubSub (the missing name in registry) in the children for the supervision tree the message is module does not exist

looks like the PubSub is hardcoded on the name Proj (if proj)

Here’s the place it’s started for a single project phoenix app:

And here for the umbrella version:

1 Like

thank you

so I did not have it because I used the flag --no-ecto and the pubSub goes with it in the template “if ecto”

No, there are <% end %> parts directly on the line with the repo declaration.

you are right but then maybe when you use mix (which I did afterwards having previously created a general mix umbrella project) you do not have the PubSub started in the supervision tree

Probably as it’s only generating the web part. This might be worth adding an issue about, so the mix task can tell people that they need to add pubsub.

all right thank you very much

I would like to add this here for future reference:

I ran into this problem while going thru Programming Phoenix 1.4 book, Chapter 11, section Extracting Rumbl and RumblWeb, page 238 very bottom.

The error encountered is this (adding to be google-able):

** (Mix) Could not start application rumbl_web: RumblWeb.Application.start(:normal, []) returned an error: shutdown: failed to start child: RumblWeb.Presence
    ** (EXIT) shutdown: failed to start child: Phoenix.Presence.Tracker
        ** (EXIT) shutdown: failed to start child: RumblWeb.Presence_shard0
            ** (EXIT) an exception was raised:
                ** (ArgumentError) unknown registry: RumblWeb.PubSub

The exact solution based on this thread seems to be editing /rumbl_umbrella/apps/rumbl/lib/rumbl/application.ex so that the children there are:

children = [
      {Phoenix.PubSub, name: Rumbl.PubSub}, # added missing piece

The file /rumbl_umbrella/config/config.exs says that the pub_sub server for rumbl_web is this

config :rumbl_web, RumblWeb.Endpoint,
  pubsub_server: Rumbl.PubSub,

so then I also changed /rumbl_umbrella/apps/rumbl_web/lib/rumbl_web/channels/presence.ex so that it starts with

use Phoenix.Presence,
    otp_app: :rumbl_web,
    pubsub_server: Rumbl.PubSub

Upon all these changes it seems to work (despite having some “connection refused” messages in console). Long story short, seems like one needs Rumbl.PubSub consistently everywhere.

Hope this helps somebody.


In addition to what Jose mentioned, I also needed to update the children in
by adding

children = [

otherwise I was getting the error:

[error] GenServer #PID<{PID}> terminating
** (ArgumentError) argument error
(stdlib 3.13) :ets.lookup(RumblWeb.Presence, :pool_size)
    (phoenix_pubsub 2.0.0) lib/phoenix/tracker.ex:311: Phoenix.Tracker.pool_size/1
    (phoenix_pubsub 2.0.0) lib/phoenix/tracker.ex:240: Phoenix.Tracker.list/2
    (phoenix 1.5.6) lib/phoenix/presence.ex:401: Phoenix.Presence.list/2
    (rumbl_web 0.1.0) lib/rumbl_web/channels/video_channel.ex:40: RumblWeb.VideoChannel.handle_info/2
    (phoenix 1.5.6) lib/phoenix/channel/server.ex:344: Phoenix.Channel.Server.handle_info/2
    (stdlib 3.13) gen_server.erl:680: :gen_server.try_dispatch/4
    (stdlib 3.13) gen_server.erl:756: :gen_server.handle_msg/6
    (stdlib 3.13) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: :after_join
State: %Phoenix.Socket{assigns: %{user_id: ...
1 Like

This is exactly what I need.
Note: I didn’t encounter “connection refused” message in the console.

1 Like

small typo there, it’s probably supposed to be RumblWeb.Presence, right?

Thank you, @josefrichter, that worked!

1 Like