ryouta

ryouta

Elixir Code Not receiving messages

This is a piece of code for testing the receive block

defmodule ElixirBasics.Runner do
  def start do
    IO.puts("Started the process at #{inspect(self())}")
    receive_msg()
  end

  def receive_msg do
    receive do
      {:ok, msg} ->
        IO.puts("received this message #{msg}")

      _ ->
        IO.puts("received invalid msg type")
    end

    receive_msg()
  end
end

ElixirBasics.Runner.start()
Process.sleep(:infinity)

This is present inside a .ex file and running with the command mix run lib/runner.ex. Now I am trying to send messages to the spawned process using Process.send(pid of process, {:ok, “hello”}, [ ]). I got :ok when trying to run the send command but the messages weren’t handled by receive block. The IO statements weren’t getting logged into the console. Can anyone point out my mistake?

Marked As Solved

meraj_enigma

meraj_enigma

This works

iex(10)> pid = spawn(ElixirBasics.Runner, :start, [])
Started the process at #PID<0.118.0>
#PID<0.118.0>
iex(11)> Process.send(pid, {:ok, “hello”}, [])
received this message hello
:ok

or this if you want to run from same file -

defmodule ElixirBasics.Runner do
  def start do
    IO.puts("Started the process at #{inspect(self())}")
    receive_msg()
  end

  def receive_msg do
    receive do
      {:ok, msg} ->
        IO.puts("received this message #{msg}")

      _ ->
        IO.puts("received invalid msg type")
    end

    receive_msg()
  end
end

pid = spawn(ElixirBasics.Runner, :start, [])
Process.send(pid, {:ok, "hello"}, [])

Also Liked

al2o3cr

al2o3cr

FWIW, Process.send will not return an error if you try to send to a PID that doesn’t exist. There are only two not-:ok values it will return, and those only happen if you ask for them explicitly (:noconnect and :nosuspend)

garrison

garrison

Note however that send() will raise if the target is a registered name which is not found, which is one of the ugliest design mistakes in OTP (and there really aren’t many).

Strangely I just noticed that this is actually not documented for the Elixir Kernel.send/2. They do at least link to the Erlang docs, but I think it should be explicitly mentioned.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement