liveBook · Manning

:net_kernel.monitor_nodes

As explained in Elixir in Action, Third Edition

:net_kernel.monitor_nodes/1 is an Erlang function that subscribes a process to notifications when remote nodes connect or disconnect.

Use `:net_kernel.monitor_nodes/1` to subscribe the calling process to remote-node connection changes. Notifications expose `{:nodeup, node}` and `{:nodedown, node}` messages, making partitions and disconnected peers observable. section 12.2.5

Choose the subscription mode deliberately:

  • `true` adds a subscription; `false` installs a single subscriber and replaces earlier subscriptions on the node. section 12.2.5
  • Use `Node.monitor/2` instead when monitoring one particular node rather than all node connections. section 12.2.5
  • Treat disconnection notifications as operational signals: disconnected nodes are not automatically reconnected. section 12.1.1
$ iex --sname node1@localhost
iex(node1@localhost)1> :net_kernel.monitor_nodes(true)

Start a distributed shell, subscribe with `:net_kernel.monitor_nodes(true)`, then observe node-up and node-down messages as peers connect or stop. section 12.2.5

How Elixir in Action, Third Edition covers it

How deep it goes
Defines subscription modes and emitted messages, then demonstrates connections and disconnections without detailing implementation internals.
Examples
Demonstrates named IEx nodes, `Node.connect/1`, `flush()`, and `{:nodeup, node}` or `{:nodedown, node}` messages.
What it assumes
Assumes familiarity with named distributed nodes and node connections, while demonstrating subscription behavior directly.
What you can do after
Diagnose disconnected peers by subscribing to node-change notifications and handling the resulting process messages.
How it connects
Connects network partitions, distributed nodes, `Node.monitor/2`, remote process monitors, and links.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.