简体   繁体   中英

Can Phoenix call an Elixir app?

I have an Elixir app running on a cluster of nodes (n1@127.0.0.1 and n2@127.0.0.1), let's call it Myapp.Server .

Inside of it, I have a module run(parameter) which goal is to contact a Rest Web Service to grab some data (parameter is the number of data to be grabbed), transform it into XML then put it into a file which will be finally transferred to an external FTP.

Calling

Myapp.Server.run(any_number)

is thus giving me the expected result.

I'm just in the beginning of learning Elixir and Phoenix, but two questions are constantly popping in my mind and despite my efforts, I can't find any hints. (or probably I'm not able to clearly understand it)

So my two questions,

Can an Elixir application (running on a node or cluster of nodes) broadcast to a Phoenix application? (for an end user to be aware of a result for example)

On the other way,

Can we initiate a request to launch an Elixir module by clicking on a button within a Phoenix page running on a different node?

Can please someone guide me on this or point me an article?

Regards,

Pierre

PS: What I'm reading so far.

  • Programming Elixir 1.2 by Dave Thomas
  • Programming Phoenix by Chris McCord, Bruce Tate and José Valim)
  • Udemy/Elixir intro from Mr. EMSON

A Phoenix app can absolutely call Elixir functions on other nodes (as long as the network is allowing the communication). Your Phoenix app is just another elixir app with nothing really special about it.

To call functions on another node, see the Process.send function, specifically where the dest is a tuple: {atom, node} . The atom is a named process on the other node and the node is the name of the node, in your case n1@127.0.0.1 . ie. send({:SomeNamedProcess, "n1@127.0.0.1"} some_message)

GenServers also can accept this {atom, node} tuple as referenced in call , cast and the server type . ie. GenServer.call({:SomeNamedProcess, "n1@127.0.0.1"}, {:do_something, some_message}) .

Additionally, you can spawn ad-hoc functions on another node using the [Node.spawn_link](e. send({:SomeNamedProcess, "n1@127.0.0.1"} some_message) ) function. ie. Node.spawn_link :"n1@127.0.0.1", fn -> IO.puts("Hello from #{inspect self}") end .

You may also choose to spawn Elixir Tasks on another node .

Choosing one of these techniques is really a matter of your exact use case.

You may abein terested in process registries as another level of indirection between the nodes:

https://m.alphasights.com/process-registry-in-elixir-a-practical-example-4500ee7c0dcc#.sws1ye9e9

https://medium.com/@StevenLeiva1/elixir-process-registries-a27f813d94e3#.5xvkv03k5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM