A Little Bit of Erlang
1 Nov, 2025
Let's do hello world.
%% File: hello.erl
%% Define hello module with a greet/0 function.
-module(hello).
-export([greet/0])
greet() ->
io:format("Hello, world!~n").
$ erl
1> c(hello). # Compile the hello module.
2> hello:greet(). # Call the greet function.
Hello, world!
ok
Communication between nodes
On Machine 1.
-module(hello).
-export([start/0]).
start() ->
register(hello, spawn(?MODULE, loop_and_wait_for_message, [])).
loop_and_wait_for_message() ->
receive
hi ->
greet(),
loop_and_wait_for_message()
end.
greet() ->
io:format("Hello, world!~n").
$ erl -name node1@pc1 -setcookie thisisasecret
1> c(hello).
2> hello:start().
true
Start the node 2 (could be on the same or another PC) and send a hi message to the first.
$ erl -name node2@pc2 -setcookie thisisasecret # Being on the same network with matching cookie make them able to communicate.
1> {hello, 'node1@pc1'} ! hi. # Send to the hello process on the node1.
Back on Machine 1, we see:
Hello, world!
Replying back
Just add where you are along with the message in a tuple ({..}) so the receiving end know where to reply back:
-module(hello).
-export([start/0]).
start() ->
register(hello, spawn(?MODULE, loop_and_wait_for_message, [])).
loop_and_wait_for_message() ->
receive
{hi, From} ->
greet(From),
From ! {reply, self()},
loop_and_wait_for_message()
{reply, From} ->
greet(From),
loop_and_wait_for_message()
end.
greet(From) ->
io:format("Hello from ~p!~n", [From]).
# Node 1
1> hello:start().
# Node 2
1> hello:start().
2> {hello, 'node1@pc1'} ! {hi, whereis(hello)}.
Then we see the messages:
# Node 1
Hello from <10359.100.0>! # 10359.100.0 is the process from node 2
# Node 2
Hello from <10358.100.0>! # 10358.100.0 is the process from node 1
In the wild
What have just been shown is Distributed Erlang. Your Erlang processes can communicate from this built-in protocol or via a custom raw TCP, HTTP, through a message broker, and a lot more. You can spawn millions of processes in a single or multiple machines and have they discover each other and work together.
Build on top of Distributed Erlang and its inherent capability of message passing, we can achieve all kind of process and node communications and supervisions with auto scaling and self-healing mechanisms (maybe via the famous OTP framework) with no need of an orchestrator like Kubernetes or Swarm.