Create two nodes:
iex --sname one
iex --sname two
Connect them:
one@ragnamac> Node.connect(:two@ragnamac)
one@ragnamac> Node.list()
Spawn the demo in node two:
two@ragnamac> c "nodedemo.exs"
two@ragnamac> pid = spawn Nodedemo, :reverse, []
two@ragnamac> send pid, "hello"
I can give a name for the process and instead of call id we can use the name
two@ragnamac> Process.register pid, :rev
two@ragnamac> send :rev, "hello"
Now I can also call this process from the node one
one@ragnamac> send {:rev, :two@ragnamac}, "hello"
"olleh " will be printed in the node two because is where IO.puts is being executed
even though the return value of the function ("hello") will be printed in node one.
If we want to send back a message we need to provide the calling pid:
two@ragnamac> pid = spawn Nodedemo, :reverse, []
two@ragnamac> Process.register pid, :rev1
two@ragnamac> send :rev1, {self(), "a phrase"}
two@ragnamac> flush()
Now is the pid the one who contains the output. To see it we need to call flush()
.