Phoenix application on a fleet of machines

50 views Asked by At

I'm developing a real-time Phoenix App using it's Channel and Socket modules. The App consists of a few processes and GenServers. I have a use case, where on an event (which is an API call from a microservice), I need to broadcast messages to all different topics on my channel on different timestamps. I have achieved this using Process.send_after(..) on my local machine for now. But my doubt is:

On a fleet of machines, because the API call will hit only a single machine in the cluster, other machines wouldn't be able to send the broadcast messages. And this would lead to inaccuracy. How can I let all the machines know of this particular event? Or am I doing it wrong?

1

There are 1 answers

0
Aleksei Matiushkin On

Assuming you know the names of nodes in the cluster, you might loop nodes, calling Node.spawn/2 on each:

def broadcast(msg) do
  Process.send_after ...
end

def broadcast_everywhere(msg) do
  Enum.each(@nodes, fn node ->
    # if not node == Node.self do
      Node.spawn node, fn ->
        Broadcaster.broadcast(msg)
      end
    # end
  end)
end

Uncomment the commented lines if the current node was already served, and [probably] somehow ensure the nodes are connected and alive upfront.

Also, Node.spawn_link/* might be worth to take a glance at.