#GenServer across Nodes

29 messages · Page 1 of 1 (latest)

split stone
#

I'm writing a GenServer intended to be called from another node. It currently looks something like this:

defmodule MyServer do
    use GenServer, restart: :transient

    def start_link(_) do
        GenServer.start_link(__MODULE__, nil, name: {:global, __MODULE__})
    end

    # handlers here
end

I've tried getting it's PID from another node (an iex shell) like this:

GenServer.whereis({:global, MyServer})
GenServer.whereis({:global, Elixir.MyServer})

neither of which have returned anything but nil. What am i missing?

sterile shuttle
#

Are the nodes connected?

split stone
#

nope. how do i connect nodes?

#

well, ill figure that part out. thanks for telling me the problem!

cold sparrow
#

do you have livebook?

split stone
#

yea

cold sparrow
split stone
#

it is.

cold sparrow
#

follow the guide in livebook

split stone
#

seen that already, but i dont see how that helps.

cold sparrow
#

did you follow it and do it or just see it?

split stone
#

i tried starting a distributed node with iex --sname test --cookie test and then in another iex with a different sname i tried Node.list which returned an empty list

cold sparrow
#

and then in livebook put test and test then hit connect

split stone
#

doesnt work.

cold sparrow
#

it does

split stone
#

just simply does nothing but making the button say "reconnect" and appending my hostname to the name

cold sparrow
#

run GenServer.whereis({:global, MyServer})

#

in your livebook

split stone
#

it works

#

but why?

#

ah figured it out from iex too.

#

now how do i connect to a node on localhost without hardcoding the hostname? it needs to be myserver@hostname for it to work.

cold sparrow
split stone
#

i believe so?

#

it works with Node.connect(:"myserver@myhostname"), but not with Node.connect(:myserver).