How to connect two Elixir nodes via local network?

前端 未结 2 1963
失恋的感觉
失恋的感觉 2020-12-23 19:16

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

2条回答
  •  不知归路
    2020-12-23 19:59

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

    In machine 1:

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

    In machine 2:

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

    Now the two machines can communicate. To test it, 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
    
    iex(node1@machine1.com)3> Node.spawn(:"node2@machine2.com", print_node_name)
    node2@machine2.com
    #PID<7789.49.0> 
    

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

提交回复
热议问题