Search code examples
pidelixirerlang-otp

Forward events from a GenEvent server to my process in elixir otp


I have a GenEvent server and I would like to be able to add several processes that get forwarded events when I send event notifications to the main server.

I can create a forwarder module and pass it a pid to send the message too, but as it seams like it must be a common pattern I was wondering if there is a better way.

In the GenEvent docs the type for a handler is

handler :: atom | {atom, term} | {pid, reference}

When I try to add a handler of the last form to a GenEvent server I always get an error.

GenEvent.add_mon_handler(pid, {self, make_ref}, [])
{:error,
 {:badarg,
  [{:erlang, :apply, [#PID<0.59.0>, :init, [[]]], []},
   {GenEvent, :do_handler, 3, [file: 'lib/gen_event.ex', line: 990]},
   {GenEvent, :do_add_handler, 5, [file: 'lib/gen_event.ex', line: 947]},
   {GenEvent, :handle_msg, 5, [file: 'lib/gen_event.ex', line: 608]},
   {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 240]}]}}

There is no further information in the elixir documentation on how to add a handler consisting of a pid and a reference. The erlang documentation for gen_event only shows it using handlers of the form atom and {atom, term}.


Solution

  • This is a documentation issue. The function GenEvent.add_mon_handler/3 does not take a handler consisting of {pid, ref} anymore. https://groups.google.com/forum/#!topic/elixir-lang-talk/gyVce092C7I