elixirnodesgen-server

Elixir - Call gen server from another node


I have two nodes on my pc (foo@my-pc and bar@my-pc).
The bar node has a simple gen-server in it:

defmodule Bar.Server do
  use ExActor.GenServer, export: :bar_server
  defstart start_link, do: initial_state(0)
  defcall get, state: state, do: reply(state)
  defcast(set(num), state: state, do: new_state(state + num))
end

The Genserver is started and running so when i run :

GenServer.call(:bar_server, :get)

it perfectly works.
I connected foo to bar by running in foo Node.connect :'bar@my-pc'
and what i expected was that now foo will be familiar with the genserver :aa in bar,
But - when i run

GenServer.call(:bar_server, :get) 

i get:

 (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started  

I'm assuming i am missing something. but can figure out what.


Solution

  • Assuming you only want a single instance of the GenServer running between the two nodes, you need to explicitly register the GenServer as global:

    use ExActor.GenServer, export: {:global, :bar_server}
    

    To communicate with this process, use the same name:

    GenServer.call({:global, :bar_server}, :get)
    

    Besides :global there are a couple of other options to register GenServer names across nodes. See the Name Registration part of the GenServer documentation for details.