owaisqayum
Sending messages between nodes
I am trying to send messages to all local nodes periodically about a leader selection. I am using 4 nodes currently
[:"n1@127.0.0.1", :"n2@127.0.0.1", :"n3@127.0.0.1", :"n4@127.0.0.1"]
Leader module
defmodule Worm.Leader do
def leader_election() do
nodes =
([Node.self()] ++ Node.list())
max = Enum.max(nodes)
Enum.each(nodes, fn node -> send_leader_info(node, max) end)
end
def send_leader_info(node, max) do
Process.send_after(node, {:leader, max}, 200)
end
end
Genserver
@impl GenServer
def handle_info({:leader, max_node}, state) do
Logger.info("--- New Leader is: #{state}")
# Reschedule once more
Worm.Leader.leader_election()
{:noreply, state}
end
I get this result
send: #Reference<0.2477235930.2055208966.145218>
send: #Reference<0.2477235930.2055208966.145221>
send: #Reference<0.2477235930.2055208966.145224>
send: #Reference<0.2477235930.2055208966.145227>
But I am not able to see any periodic message on any connected node. What am I doing wrong here?
Thanks
Most Liked
hauleth
You are sending messages, but you have never specified to which process you want to send them. This mean, that it treats it as an process names in local registry, so in the end these messages goes nowhere.
mpope
I recommend you take a look at the erlang Erlang -- pg module. It sounds like it’d fit this use case well, getting registered processes across nodes to send them messages.
mpope
If you’re on Erlang 23 or newer the pg module is what should be used. pg2 is deprecated and will be removed soon.
Aetherus
Process.send_after/3 needs a pid or a process name as the first argument, but you gave it a node name.
Here’s part the documentation of Process.send_after/3:
If
dest(the first arg) is a PID, it must be the PID of a local process, dead or alive. Ifdest
is an atom, it must be the name of a registered PROCESS which is looked up at
the time of delivery. No error is produced if the name does not refer to a
process.
owaisqayum
How can i specify processes on a node, as I need the messages to display on other nodes like a heartbeat.








