user5248284
user5248284

Reputation: 45

How do I send a message to multiple processes

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?

Upvotes: 1

Views: 1236

Answers (3)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

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.

Upvotes: 1

I GIVE TERRIBLE ADVICE
I GIVE TERRIBLE ADVICE

Reputation: 9648

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

Upvotes: 4

GavinBrelstaff
GavinBrelstaff

Reputation: 3069

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

Upvotes: 3

Related Questions