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 pg — OTP 29.0.2 (kernel 11.0.2) 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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









