4 likes
·
2.3K reads
2 comments
I'm still a beginner, but I was wondering why :pg is used to get all the PIDs of the PGServer processes in the cluster and send them messages (e.g., {:broadcast_to_local, topic, message}). Why not use something like Enum.each(Node.list(), fn node -> Hermes.broadcast_local(topic, message) end) instead? I'm sure there's a good reason, I'm just trying to understand how it all works.
Hello Dan, you are right, you can also achieve this behavior by sending directly to nodes on simple scenarios. Although, :pg has more stuff you could probably use like scoping. One issue I'm seeing in this approach is the caller will be the one to decide which node will receive the message instead of allowing nodes to decide whether they want to receive messages or not. For example, you have a cluster of nodes. If you want to use Node.list + Node.spawn + Hermes.boradcast_local, you need to make sure that all nodes in the cluster has Hermes
. If you use :pg, a node that wants to be part of the pubsub just needs to have a PGServer running. If the node doesn't want to participate, the node can still be part of the cluster, even without PGServer running.