Search code examples
erlangerlang-shell

How to code load modules on all nodes in Distributed Erlang?


I am learning Erlang/OTP, and in Chapter 14 it says:

"you can load Module to all nodes use shell command : command nl(Mod)".

I want to know, how to do this? I can't find the shell command : command nl(Mod).


Solution

  • You tried to type help(). in shell? This function is defined in c module in stdlib. But shell will understand nl(Mod).

    To perform test, start at least two erlang vms and connect it:

    erl -pa your_module_path -name [email protected]
    erl -name [email protected]
    

    in first shell connect to second node by:

    net_kernel:connect('[email protected]').
    

    should respond with true.

    In your_module_path create some simple module like:

    -module(nl_test).
    -export([test/0]).
    test() -> 1.
    

    and compile it by c(nl_test). Now you can run nl_test:test() from first shell but the second one can't load it. To broadcast it and force load, just run from first nl(nl_test). Now try from second shell nl_test:test().

    Let's say you modified test() -> 1. to test() -> 2. and all you need to do is to just run c(nl_test), nl(nl_test). or shorter nc(nl_test) which means the same.

    nl_test:test(). on all your connected nodes should return 2.

    This may look very easy but in complicated production systems, proper hot-loading is quite advanced problem.