Run a function on each node in the cluster


I have a small cluster of Elixir nodes and I want to run a function on each node in the cluster and display the output on a liveview page. How’s the best way to go about running the function on each node in Node.list(), awaiting the response from each one? I’ve seen some examples using Node.spawn, and I know there’s an async/await syntax in Elixir. Looking for some guidance. Thanks!

There are functions in :rpc module, :rpc.cast, :rpc.multicall etc. see Erlang -- rpc
Can be used as

{results, failed_nodes} = :rpc.multicall(Node.list(), mod, fun, args)

Could you please tell more about the scenario, the connection between the user interaction through liveview and distributed nodes? To give an idea why I’m asking is, for example, sometimes all we need is to broadcast data updates that happened on one node to all users connected to any node in the cluster through liveview, in that case Phoenix.PubSub broadcast could be enough and no need to manually call a function on each node. But YMMV =)


I’m trying to collect information from a log file on each node in the cluster and show the results on a liveview page. I want to enter some information on which to base a search, have each node run a search and report back the results. I don’t think Phoenix.PubSub comes into play here.

Phoenix.PubSub could be used for this too. You could have a process running on each node that subscribes to a topic called “log_tailers”. Then you broadcast on said topic something like “give me the last 10 lines” and all subscribed processes would return to you the relevant info. :rpc and friends work too.


I think the :rpc.multicall is exactly what I want, but I can’t get it working. I’m trying this:

:rpc.multicall(Node.list(), :"Elixir.Appapi.Log.FindPayload", :start, '13102496')

but getting an error:

   badrpc: {:EXIT,
    {:undef, [{Appapi.Log.FindPayload, :start, '13102496', []}]}}
 ], []}

This is the call in IEx


What am I missing?

I am guessing here, two possible reasons:

  1. The fourth argument to multicall needs to be list of parameters, so you need: :rpc.multicall(Node.list(), Appapi.Log.FindPayload, :start, ['13102496'])

  2. The module Appapi.Log.FindPayload is not loaded in the remote node

It was the arguments being in a list. Thank you @josevalim!