Reputation: 151
I have a function that does the broadcasting:
broadcast(Msg, Reason) ->
Fun = fun(P) -> P ! {self(), Msg, Reason} end, %line 27
lists:foreach(Fun, nodes()).
But it's not working,I get this error:
=ERROR REPORT==== 12-Apr-2014::15:42:23 ===
Error in process <0.45.0> on node 'sub@Molly' with exit value: {badarg,[{subscri
ber,'-broadcast/2-fun-0-',3,[{file,"subscriber.erl"},{line,27}]},{lists,foreach,
2,[{file,"lists.erl"},{line,1323}]},{subscriber,loop,0,[{file,"subscriber.erl"},
{line,38}]}]}
Line 38 is a line where I call the function
broadcast(Reason, Msg)
I can't wrap my head around the error. Why doesn't this work?
Upvotes: 1
Views: 83
Reputation: 41568
!
takes the same arguments as erlang:send/2. The documentation specifies that the target can be one of:
{RegName, Node}
, for a process registered on a remote nodeYou're sending messages to the elements of the return value of nodes()
. These are atoms, but they are node names, not locally registered processes. If the process you want to send the message to is registered as foo
on the remote node, write {foo, P} ! {self(), Msg, Reason}
instead.
On the other hand, if you have the pids of the processes on the remote node, there is no need to specify the node name, as the pids contain that information. Just send the message to the remote pid as you would for a local pid.
Upvotes: 2
Reputation: 2593
Node is just an atom, you can't send a message to it. What you need is a pid on that node. For example it could be a registered process and the pid could be obtained by calling rpc:call(Node, erlang, where, [Name]). Another option could be to use gproc.
Upvotes: 2