Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect two Elixir nodes via local network?

Tags:

elixir

How can I connect two Erlang/Elixir-nodes of two different machines via network connection?

like image 741
ChaosSteffen Avatar asked Jun 27 '13 19:06

ChaosSteffen


People also ask

How do you connect two nodes together?

1. Drag one node over the input or output of a second node, and release the mouse button to establish a connection. 2. Click on an empty space in the Node Graph to then place the node there.

What are elixir nodes?

An ELIXIR Node is a collection of research institutes within a member country. ELIXIR Nodes run the resources and services that are part of ELIXIR. Each Node has a lead institute that oversees the work of that Node.


2 Answers

You have to name your nodes and use the same cookie on both nodes.

In machine 1:

iex --name [email protected] --cookie a_cookie_string 

In machine 2:

iex --name [email protected] --cookie a_cookie_string 

Now the two machines can communicate. To test it, you can do something like this, on machine1:

iex([email protected])1> Node.connect :"[email protected]" true  iex([email protected])2> print_node_name = fn -> IO.puts Node.self end #Function<erl_eval.20.80484245>  iex([email protected])3> Node.spawn(:"[email protected]", print_node_name) [email protected] #PID<7789.49.0>  

Domain names machine1.com and machine2.com can be changed with the ip addresses of the machines as well.

like image 162
Riccardo Marotti Avatar answered Oct 06 '22 10:10

Riccardo Marotti


If you are trying to connect Nodes via code: You need to turn your running code into a distributed node. To do this run Node.start(:fullNameOfServer).

Eg: if your IP is 192.168.0.1, you can have a full Node name like :"[email protected]"

Once you turn your node into a distributed node, you set the cookie: Node.set_cookie :cookie_name

Finally, you need to establish a connection with the remote Node. (You also need to Node.start and Node.set_cookie on the remote node) To do this, you need the remote node's name. Let us assume the remote node's name is [email protected] (assuming this Node is another computer on the same local network). The code to do this, looks like Node.connect :"[email protected]"

You can now run Node.list to see the [email protected] available on [email protected] and vice versa.

Summarising the above points, your code should look something like

On the Foo machine

Node.start :"[email protected]" #this is the IP of the machine on which you run the code Node.set_cookie :cookie_name Node.connect "[email protected]" 

On the Bar machine

Node.start :"[email protected]"  Node.set_cookie :cookie_name 
like image 36
adityah Avatar answered Oct 06 '22 11:10

adityah