Skip to content

Instantly share code, notes, and snippets.

@scottmascio2115
Created January 4, 2017 00:06
Show Gist options
  • Save scottmascio2115/3d380cd144fed8703b841696dc86eb69 to your computer and use it in GitHub Desktop.
Save scottmascio2115/3d380cd144fed8703b841696dc86eb69 to your computer and use it in GitHub Desktop.
## Distributed code using Nodes
# Elixir docs http://elixir-lang.org/getting-started/mix-otp/distributed-tasks-and-configuration.html
# This blog post http://benjamintan.io/blog/2014/05/25/connecting-elixir-nodes-on-the-same-lan/
iex --sname foo
Results in
foo@computer-name
Define a module in that shell
iex> defmodule Hello do
...> def world, do: IO.puts "hello world"
...> end
Open a new terminal window
iex --sname bar
Inside the bar terminal window
iex> Hello.world
Reuslts in
(UndefinedFunctionError) undefined function: Hello.world/0
Hello.world()
In the bar terminal
iex> Node.spawn_link :"foo@computer-name", fn -> Hello.world end
#PID<9014.59.0>
hello world
Elixir spawned a process on another node and returned its pid
We can send and receive message from the pid returned by Node.spawn_link/2 as usual.
We can have the two processes talk to each other like any other processes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment