Elixir: Clustering Elixir Nodes - Updating Shared State. 

angelcool@fedora-laptop$ # Let's get the date and versions used
angelcool@fedora-laptop$date
Sat Apr 6 11:51:08 AM PDT 2024
angelcool@fedora-laptop$iex --version
IEx 1.15.7 (compiled with Erlang/OTP 26)
angelcool@fedora-laptop$mix --version
Erlang/OTP 26 [erts-14.2.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Mix 1.15.7 (compiled with Erlang/OTP 26)
angelcool@fedora-laptop$mix phx.new --version
Phoenix installer v1.7.11
angelcool@fedora-laptop$
angelcool@fedora-laptop$
angelcool@fedora-laptop$
angelcool@fedora-laptop$ # The gist of this post...
angelcool@fedora-laptop$
angelcool@fedora-laptop$ # Terminal 1
angelcool@fedora-laptop$ iex --name a@127.0.0.1
iex(a@127.0.0.1)1>

angelcool@fedora-laptop$ #Terminal 2
angelcool@fedora-laptop$ iex --name b@127.0.0.1
Erlang/OTP 26 [erts-14.2.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.15.7) - press Ctrl+C to exit (type h() ENTER for help)
iex(b@127.0.0.1)1> Node.connect :"a@127.0.0.1"
true
iex(b@127.0.0.1)2> Node.list
[:"a@127.0.0.1"]
iex(b@127.0.0.1)3>

# Terminal 1
iex(a@127.0.0.1)> Agent.start(
fn -> %{hello: "world"} end,
name: {:global, GlobalAgent}
)
{:ok, #PID<0.116.0>}

# Terminal 2
iex(b@127.0.0.1)4> Agent.get {:global,GlobalAgent}, &(&1)
%{hello: "world"}

# Terminal 2 - Update state
iex(b@127.0.0.1)5> Agent.update {:global, GlobalAgent}, fn _ -> %{hello: "everyone!"} end
:ok
iex(b@127.0.0.1)6>

# Terminal 2 - Get new state
iex(b@127.0.0.1)6> Agent.get {:global,GlobalAgent}, &(&1)
%{hello: "everyone!"}

# Terminal 1 - Get new state
iex(a@127.0.0.1)2> Agent.get {:global,GlobalAgent}, &(&1)
%{hello: "everyone!"}


Comments
Comments are not available for this entry.
2024 By Angel Cool