Skip to content

Instantly share code, notes, and snippets.

@pdgonzalez872
Last active May 1, 2019 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdgonzalez872/61883f8968dd04394cea994326863b78 to your computer and use it in GitHub Desktop.
Save pdgonzalez872/61883f8968dd04394cea994326863b78 to your computer and use it in GitHub Desktop.
`global` exercise

global demonstration

Create named iex instances

On terminal 1: $ iex --name t1@127.0.0.1

On terminal 2: $ iex --name t2@127.0.0.1

Connect instances together

From either t1 or t2, connect to the other one.

# from t2
iex> Node.connect(:"t1@127.0.0.1")
true

Assert we are connected:

Check from either instance.

# from t2
iex> Node.list()
[:"t1@127.0.0.1"]

# from t1
iex> Node.list()
[:"t2@127.0.0.1"]
# from `t1`
Agent.start_link(fn -> %{t1: true} end, name: {:global, {:t1, "t1 description"}})
# from `t2`
Agent.get({:global, {:t1, "t1 description"}}, & &1)

Terminal outputs:

t1:

~ :> iex --name t1@127.0.0.1
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]

Interactive Elixir (1.7.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(t1@127.0.0.1)1> Node.list()
[:"t2@127.0.0.1"]
iex(t1@127.0.0.1)2> Agent.start_link(fn -> %{t1: true} end, name: {:global, {:t1, "t1 description"}})
{:ok, #PID<0.112.0>}
iex(t1@127.0.0.1)3>

t2:

~ :> iex --name t2@127.0.0.1
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]

Interactive Elixir (1.7.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(t2@127.0.0.1)1> Node.connect(:"t1@127.0.0.1")
true
iex(t2@127.0.0.1)2> Agent.get({:global, {:t1, "t1 description"}}, & &1)
%{t1: true}
iex(t2@127.0.0.1)3>

Visual

|--------|    |--------|
|   t1   |----|   t2   |
|--------|    |--------|
|        |    |        |
|   At1  |    |        |
|        |    |        |
|--------|    |--------|
  • At1 -> Agent named t1

Conclusion:

t2 can communicate with At1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment