axelson

axelson

Scenic Core Team

I am working on an application that is structured as an umbrella application. Currently there are only two applications in the umbrella, one really small application and one that is quite large/monolithic Phoenix app that we are in the process of breaking up into multiple apps.

Is there a best practice for calling from one application to another without defining a direct dependency between the two?

The reason that I want to do this is that I want to avoid a circular dependency among the applications, but I still want app B to be able to notify app A that a particular event has occurred (i.e. app B does not need to receive a response). I believe I could do this by utilizing Phoenix Pubsub but I am wary of relying too heavily on that approach because in my experience it can cause “action at a distance” type of problems or otherwise make control flow hard to follow, especially if a subscriber then publishes another event.

Am I barking up the wrong tree? Should I use something like GenServer.cast to notify app A from app B?

Showing Posts 1 to 5

idi527

idi527

A bit off-topic, but instead of phoenix’s pubsub, you can create a local pubsub out of Registry, make it your third app under “umbrella”, and communicate inside the node through it. That’s what I do, but “action at a distance” is definitely a problem.

To alleviate it to some extent, you can declare macros (or functions) that return topic names for every possible subscription inside a single place (local pubsub, for example).

defmodule PubSub.Topics do
  # not sure if it compiles
  defmacro subject_update(subject_id) do
    quote(do: "subject_update:#{unquote(subject_id)}")
  end
end

# and then in other apps
require PubSub.Topics
PubSub.publish(PubSub.Topics.subject_update(subject_id), %Subject.Update{...})

or maybe go even further and define a pubsub submodule for any possible communication

defmodule PubSub.Subject do
  def publish_update(...) do 
    # ...
  end
end
OvermindDL1

OvermindDL1

What I do and have always done going back to erlang days (though with Elixir you can macro-inline calls) is thus:

Define my behaviours in a base library, depend on it (I may have multiple behaviours in multiple dependencies even). In the Application.get_env stuff (settable from Elixir via config.exs) get which module is being used if it can be backed in, if it is more ‘flowing’ and not baked in then pass the module name as an argument through the calls. This also make it really easy to swap out implementations as you need, including one specific for tests and more too.

I don’t really use umbrella’s though, but rather have each application be a dependency of an overall main application that otherwise does nothing but set up all the links between them.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I think so. I mean if A depends on B, but B can call A’s code, then B also depends on A and you’ve got a circular dependency whether you’ve written it down in mix.exs or not.

Phoenix.PubSub is both a bit heavy weight, and also I think the wrong paradigm. What you need are event handlers, which can happen with basically just pure data, the process based subscription model of Phoenix.PubSub doesn’t make sense here. Simple version basically just does the following.

Have A configure b with an event handler module found in A:

# in app A's config

config :b, event_handlers: [SomeModuleInA]

Then in B, instead of calling A’s code, grab event handlers and call a function:

for handler <- Application.get_env(:b, :event_handlers) do
  handler.dispatch(:foo_created, some_data)
end

We’ve done this in our umbrella apps and it’s worked pretty nicely. It avoids a circular dependency, while still avoiding a “broadcast to the winds and who knows who responds” kind of situation, since it’s pretty easy to keep track of the handlers list.

OvermindDL1

OvermindDL1

Yep! This is what I was proposing! Thanks for the specific example, I’m in a bit of a rush this afternoon. ^.^

axelson

axelson OP

Scenic Core Team

@benwilson512 thanks! That approach looks pretty neat. I think I’ll try to combine it with a shared behaviour as well.

Also I’ll probably tweak the config so that multiple applications can register a handler. So there will be lines like

# in apps/a/config/config.exs
config :b, :event_handlers, [a: [SomeModuleInA]]

# in apps/c/config/config.exs
config :b, :event_handlers, [c: [SomeModuleInC]]

Thanks for the suggestions!

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews