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 Erlang -- pg 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.

Aetherus

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. If dest
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

owaisqayum

How can i specify processes on a node, as I need the messages to display on other nodes like a heartbeat.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement