Elixir Connecting Nodes
Node.connect() asks the current node to join a named node. In a plain script the attempt is ignored, because the current node has no distributed name.
A connect attempt to a non-distributed node is ignored
Node.connect() asks this node to join another node. When the current node has no distributed name, the attempt returns :ignored.
IO.inspect(Node.connect(Node.self()))Ping reports an unreachable node
Node.ping() checks whether a node answers. A node that is not reachable answers :pang.
IO.inspect(Node.ping(Node.self()))
IO.inspect(Node.ping(:"ghost@nohost"))An ignored attempt leaves the node list empty
An ignored attempt adds nothing. Node.list() still returns an empty list.
Node.connect(Node.self())
IO.inspect(Node.list())
IO.inspect(Node.alive?())Example
IO.inspect(Node.alive?())
IO.inspect(Node.connect(Node.self()))
IO.inspect(Node.ping(Node.self()))
IO.inspect(Node.list())Key points
- Node.connect(): asks the current node to join the named node.
- Ignored: :ignored means the current node is not running distributed, so the request is set aside.
- Node.ping(): checks whether a named node answers.
- Pang: :pang means the named node did not answer.
- Node.list(): the nodes this node is connected to.
In this example, the script checks whether it is alive, tries to join itself, pings itself, and lists its nodes. Every answer shows the same fact, that this lone node is not part of a cluster.
Note
a real cluster needs nodes started with a name, so this lesson proves only the single-node answers.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does Node.connect(Node.self()) return in a plain script?