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?
The first thing I gonna tell you, is really handy tip.
There are three ways to exit Erlang’ shell:
- “classic”: hit
Ctrl + C, then press
- “UNIX-way”: two times hit
Ctrl + C
- “Erlang-way”: simply type
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
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
C:\Windows\system32\drivers\etc\hosts on my girlfriend’s laptop I added this:
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:\Users\username\ directory. In Linux that’s in
So, short summary on what you should have to run distributely in Erlang:
- hostname(-s) of other node(-s) in your
- same cookie for all your nodes in
- running instances with corresponding shortnames and hosts
To show some code, I wrote this short module:
Now, let’s start that!
First, I registered that process with some name on the
And then, the only thing I needed to do - is just send messages from node
Much cooler than writing ping-pong programs, huh? =)