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

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

We're in Beta

About us Mission Statement