SoFunction
Updated on 2025-04-08

Example of registration process usage in Erlang distributed node

Previous articleThe article says that the atoms associated with the registration process have a global scope. The global here refers to the current Erlang virtual machine. In the distributed form, it is the current distributed node. Therefore, the atoms associated with the registration process in one node cannot be used directly in another node, but must be used with the target node.

Copy the codeThe code is as follows:

{RegName, Node} ! {messages}.

example

Start a Server node first

Copy the codeThe code is as follows:

erl -sname server

Then operate in Erlang Shell

A brief introduction to some common functions

Copy the codeThe code is as follows:

% View the current node
node().
% => 'server@Gentoo-PC'

% View all connected nodes
nodes().
% => []                                                                                                                                                                                                                                                            �

% Check whether the current node is alive
is_alive().
% => true


Then go to the main topic
Copy the codeThe code is as follows:

% Start the last program in the previous article
test:start().
% Waiting for new message.
% => true

% The current node can use the testp atom
testp ! message.
% New message: message
% Waiting for new message.
% => message


Then start another Client node
Copy the codeThe code is as follows:

erl -sname client

In the new Erlang Shell
Copy the codeThe code is as follows:

nodes().
% => []                                                                                                                            �

% The current node cannot use this atom directly
testp ! {}.
% ** exception error: bad argument
%      in operator  !/2
%         called as testp ! {}

% Need to be used with the target node
{testp, 'server@Gentoo-PC'} ! {}.
% => {}


At this time, the server node will receive the message and print it out
Copy the codeThe code is as follows:

% New message: {}
% Waiting for new message.

After the first connection between nodes, the two nodes will remain connected

In Client node

Copy the codeThe code is as follows:

nodes().
% => ['server@Gentoo-PC']

In Server node
Copy the codeThe code is as follows:

nodes().
% => ['client@Gentoo-PC']

Ending

Of course, this is just a method. Since the call function is defined in the module, you can use remote calls to call the test:call method on the Server node.

Functions can be called remotely using the call/4 method in the rpc module

Copy the codeThe code is as follows:

%Execute apply(Module, Function, Args) on Node node
% Returns Result when the call is successful, {badrpc, Reason} when the call fails
-spec rpc:call(Node, Module, Function, Args} -> Result | {badrpc, Reason}

In Client node
Copy the codeThe code is as follows:

rpc:call('server@Gentoo-PC', test, call, ['message from other node']).