How to create an application and run it another

Hi, I’m unsure how to form the title of the question (or the question overall) so I’m writing the best I can.

Until a bit ago I’ve only made Phoenix applications which didn’t have any separated functionalities per se outside of the app scope. Everything was in that Phoenix application.

Now I want to create an application which runs as a Discord bot independently but maybe at a later point use Phoenix with it. But instead of turning my Elixir app into a Phoenix application eventually, I’d like to create my Elixir app, put it on GitHub and then eventually use a completely new Phoenix app and call my Discord app as a hex dependency and run it under applications in mix.exs.

I have no clue how to google this properly nor find any info which seperated a normal elixir app than an api app (or how do I call it).

Write your bot, publish it to hex, refer to it as dependency in the phoenix project.

Thats exactly how you do it. You already described it in your post.

I meant it as a more technical part. Should I make a file under lib/my_app/application.ex and add it in mix.exs as a mod application? Like so:

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      mod: {MyApp.Application, []},
      extra_applications: [:logger]
    ]
  end

I did use the Application module (behavior?) in MyApp.Application.

I just don’t know if that would work ahahahah.

Totally depends.

Do you want to have the bot in its own application supervision tree, single instance? Or do you want it to be used and started as a subtree in the “host” applications supervision tree?

If you want it to run its own tree you need a :mod entry, if you want it to be a subtree, you don’t need it.

1 Like

I actually want to make it run independently but could as well run if if was a dependency (specified in mix.exs), so I added it as a :mod entry. I think it is the appropriate solution, but, if it’s not I’ll be sure to update this thread!

Every application can be run as dependency, the :mod entry has nothing to do with it.

:mod only determines whether or not an application has an entrypoint and therefore its own supervision tree.

1 Like