How to connect two Elixir nodes through a local network?

How can I connect two Erlang / Elixir nodes of two different computers through a network connection?

+47
elixir
Jun 27 '13 at 19:43
source share
2 answers

You must name your sites and use the same cookie on both sites.

In car 1:

iex --name node1@machine1.com --cookie a_cookie_string 

In car 2:

 iex --name node2@machine2.com --cookie a_cookie_string 

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

 iex(node1@machine1.com)1> Node.connect :"node2@machine2.com" true iex(node1@machine1.com)2> print_node_name = fn -> IO.puts Node.self end #Function<erl_eval.20.80484245> iex(node1@machine1.com)3> Node.spawn(:"node2@machine2.com", print_node_name) node2@machine2.com #PID<7789.49.0> 

The machine1.com names machine1.com and machine2.com can be changed using the IP addresses of the machines.

+57
Jun 28 '13 at 8:50
source share

If you are trying to connect nodes through code: you need to turn your code into a distributed node. To do this, run Node.start(:fullNameOfServer) .

For example: if your IP address is 192.168.0.1, you can have the full name Node, for example :"foo@192.168.0.1"

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

Finally, you need to establish a connection with the remote node. (You also need node.start and node.set_cookie on the node remote control). To do this, you will need the remote name Node. Suppose the remote Node name is bar@192.168.0.2 (it is assumed that this Node is another computer on the same local network). The code for this looks like Node.connect :"bar@192.168.0.2"

Now you can run node.list to see bar@192.168.0.2 at foo@192.168.0.1 and vice versa.

To summarize the above points, your code should look something like this:

By car foo

 Node.start :"foo@192.168.0.1" #this is the IP of the machine on which you run the code Node.set_cookie :cookie_name Node.connect "bar@192.168.0.2" 

By car with a bar

 Node.start :"bar@192.168.0.2" Node.set_cookie :cookie_name 
+1
Sep 15 '17 at 4:51 on
source share



All Articles