1

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 Answers3

4

There is no way that does not involve iteration, whether direct (you write it) or indirect (a lib does it for you)

I GIVE TERRIBLE ADVICE
  • 9,578
  • 2
  • 32
  • 40
3

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

GavinBrelstaff
  • 3,016
  • 2
  • 21
  • 39
1

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.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160