First time I faced functional programming, I was impressed. Just a bit. That was in 2012. The second time, I was studying real functional programming at the university. Today was the final test.

We were taught many interesting things about functional programming and lot of things about Haskell. But there were only two lectures and two practices on Erlang. And nothing was told about its distributed programming abilities.

I was a bit disappointed by this fact. So, I turned on my girlfriend’s laptop, installed Erlang and created this short intro to distributed programming in Erlang.


This short intro does not include Erlang tutorial and requires you to have at least two machines - either Virtual or hardware, if you wish. Even that does not really matter!

How to get out of a train?

Gordon Freeman trying to get out of a train

The first thing I gonna tell you, is really handy tip.

There are three ways to exit Erlang’ shell:

  1. “classic”: hit Ctrl + C, then press a (Abort) and Return
  2. “UNIX-way”: two times hit Ctrl + C
  3. “Erlang-way”: simply type q(). and hit Return

You’ll be happy to know ‘bout last two - they are just easier!


So, let’s just dive into distributed programming! First thing you’ll gonna need - is to know your machines’ IP addresses. Then you’ll gonna need to point each of them to the other one - just set each other’s hostname in the /etc/hosts file (for Windows it’s C:\Windows\system32\drivers\etc\hosts).

I have had two laptops I named moonode (my laptop) and foo (my girlfriend’s laptop). So, on my Ubuntu, I added this line to /etc/hosts:    foonode

And in C:\Windows\system32\drivers\etc\hosts on my girlfriend’s laptop I added this:   moonode

hosts in Windows

Note: I was sitting at home, so laptops were connected just to my home WiFi router. And that’s great news for enyone, who wants to try that at home!

Each Erlang instance was run with the corresponding shortname of machine: erl -sname moo@moonode and erl -sname foo@foonode.

Both machines should have the same cookie to communicate. That’s basic Erlang security, for your great good. Cookie is just a upper-cased word, stored in a .erlang.cookie file. For Windows, that file is in the C:\Windows\ or C:\Users\username\ directory. In Linux that’s in /home/username/ directory.


So, short summary on what you should have to run distributely in Erlang:

  1. hostname(-s) of other node(-s) in your hosts file
  2. same cookie for all your nodes in .erlang.cookie file
  3. running instances with corresponding shortnames and hosts

Running stuff

To show some code, I wrote this short module:

-export([ start/0 ]).

start() ->
        { msg, M } ->
            io:format(">> ~s~n", [ M ]),

        finish ->
            io:format("< finish received >~n"),

Now, let’s start that!

First, I registered that process with some name on the foo node:

register(foo_pid, spawn(test1, start, [])).

And then, the only thing I needed to do - is just send messages from node moo!

{ foo_pid, foo@foonode } ! { msg, "Obey!" }.



Much cooler than writing ping-pong programs, huh? =)