happy songs
happy songs

Reputation: 873

Send message to all processes

I'm new to Elixir, I have a bunch of running processes and I need to broadcast a message to all of them from another process. After consulting the documentation for Process I still can't figure out how to achieve that.

It might not be the best comparison, but is there some function like "notifyAll" from Java ?

Upvotes: 0

Views: 653

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

This approach (described in comments) is vulnerable to atoms DDOS in the first place. 2M of skeletons would kill your Erlang VM, because atoms are not garbage collected. Also, spawning not monitored processes is not a good idea as stated in Kernel.spawn/3.

Usually, you use DynamicSupervisor to supervise children and it exposes which_children/1 that allows easy enumeration and sending messages to all of them.

Another approach would be to use :pg module. Make your skeletons to join the process group and send messages to all the members.

One might also use Phoenix.PubSub abstraction on top of :pg or Envio to ease PubSub pattern implementation.


If you still want your initial approach with spawn/3 to work, start the dedicated process that would keep track of all the skeletons (they might send it a “register-like” message upon start) and implement iteration through all the spawned skeletons within this process, sending all a message.

Upvotes: 2

Related Questions