Q:

In Elixir/Erlang, which port is used for `Node.connect`?

One can init a Elixir node by iex using:
iex --sname node1@10.99.1.50 --cookie foo

And then another one can connect this node in REPL using:
Node.connect(:"node1@10.99.1.50")

It seems the connection is over TCP protocol. However, I didn't find a parameter in document to specify which port is used. Does anyone have any ideas about this?

A:

Connecting nodes is handled by the Erlang Port Mapping Daemon (epmd) which runs by default on port 4369. From the documentation:

A different port can be specified to allow several instances of epmd, representing independent clusters of nodes, to co-exist on the same host. All nodes in a cluster must use the same epmd port number.

The actual node opens a random(?) port and announces this along with its sname to the local epmd. When you now connect to 'node1@10.99.1.50', your Erlang VM will ask the remote epmd running on 10.99.1.50 on port 4369 for information on 'node1'. It will answer with the actual port number to which your process then connects directly.

A:

As per this post Erlang Distribution Erlang uses port 4369 over TCP. The post has more information about this.

For the firewall: Erlang distribution uses port 4369 for epmd as well as random ports for each node. You can limit the range of these random ports by using Erlang kernel application environment settings inet_dist_listen_min and inet_dist_listen_max. You will need to allow incoming TCP connections on these ports, but only from other hosts of the cluster.

You may also be interested in this: Chris McCoord on the subject

