axelson

axelson

Scenic Core Team

Best practice for calling a function in another application without defining a dependency

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?

Marked As Solved

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.

Also Liked

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.

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

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!

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

We're in Beta

About us Mission Statement