Connecting nodes in Elixir

This post only really covers me on a local network, but the same principles apply to remote network connections, just make sure to use the externally facing IP address. All communications are unencrypted by default, and in a future post I may cover how to encrypt communications.

The two computers used were: a MacBook Pro (Early 2011) running 10.10.3 with a Core i5 dual core processor and an Acer Aspire V3 running Windows 8.1 with a Core i7. Both have 8 GB of memory.

the Windows computer has Erlang/OTP 17.4 and the Mac has Erlang/OTP 18, and both have the master branch builds of Elixir as of today.

Onward…

Open up iTerm on Mac (or Terminal if you’re a fake programmer)
cmd for Windows (yeah yeah I have cygwin but I like to make my life more difficult).

run iex on both.

Find your ip address (you can use the private ip address on your local network)

On the Mac I typed the following:

➜  ~  iex --name mac@169.254.235.38 --cookie monsta
Interactive Elixir (1.1.0-dev) - press Ctrl+C to exit (type h() ENTER for help)

iex(mac@169.254.235.38)1> :global.register_name :mac, :erlang.group_leader

On the Acer I typed the following:

C:\Users\arthur>iex --name win@192.168.1.255 --cookie monsta
Interactive Elixir (1.1.0-dev) - press Ctrl+C to exit (type h() ENTER for help)

iex(win@192.168.1.7)1> :global.register_name :win, :erlang.group_leader

Both should output :yes (this is an atom if you are unfamiliar with Elixir. In Erlang this atom would be represented as yes but Elixir has Ruby-like syntax with some important differences and they are sort of like symbols).

Then, you need to connect both running iex processes (they are Nodes) together.

On the Mac I typed the following:

iex(mac@169.254.235.38)2> Node.connect :"win@192.168.1.7"
==> true

and on the Acer I typed the following:

iex(win@192.168.1.7)2> Node.connect :"mac@169.254.235.38"
==> true

Both of these commands on their respective shells should return true. If one or the other fails it is probably because the destination Node that you’re trying to connect to was started up (i.e., remember the iex --name x@y command?) with a local IP and not the external IP. For some odd reason I had this problem as I was writing this quick tutorial even though both machines are on the same network.

Last step before the fun!

You have to grab the process identifier of the other Node.

On OS X, I had to run this command:

iex(mac@169.254.235.38)3> win = :global.whereis_name :win
==> #PID<8931.9.0>
iex(win@192.168.1.7)3> mac = :global.whereis_name :mac
==> #PID<9131.9.0>

So you could do some pretty magical stuff at this point, but just as a tease, try this:

mac |> IO.puts "Hey Mac, from Windows"
win |> IO.puts "Hey Win, from Mac"

This should cause your strings to print out in the other machine’s iex session! Cool!!!

This also worked on a DigitalOcean instance I spun up from both of my machines. Awesome stuff.

Feel free to shoot me any questions if you have any trouble. I’m @arthurcolle on the twitters and you can find me on freenode on #elixir-lang and #erlang.

Thanks for reading!