owaisqayum

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

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

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

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.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement