I need to send a message to multiple processes at once. Is there a way to do this, without looping over all the PID's and sending the message to each process individually?
3 Answers
There is no way that does not involve iteration, whether direct (you write it) or indirect (a lib does it for you)

- 9,578
- 2
- 32
- 40
You can do it in series with a for
loop
for pid <- listofPids do
send pid, {:message, self() }
end
the self()
provides the receiving process with the pid of the message sender.
Glossary: pid = process id
If you are prepared to call Erlang from Elixir there is a relevant question here: Erlang Multicast

- 3,016
- 2
- 21
- 39
-
Yes of course, but I wondered if there was a better way, similar to a UDP broadcast, or something like that. – user5248284 Dec 22 '18 at 07:45
-
best to have put that in your title, then. – GavinBrelstaff Dec 22 '18 at 07:50
-
sorry, I thought I had made it clear in the question. – user5248284 Dec 22 '18 at 08:04
-
Have a look at this Erlang example that could be executed from Elixir https://stackoverflow.com/questions/78826/erlang-multicast – GavinBrelstaff Dec 22 '18 at 09:19
It depends on what do you call “message.” For the message that is passed to the process mailbox, the answers are already given.
Also, there is Elixir core Registry
module that implements exactly what do you want (basically PubSub
.)
If that is a possible way for you to go, you might want to look at Envío
library I wrote to simplify publishers and subscribers creation and eliminate all the boilerplate needed.

- 119,336
- 10
- 100
- 160