Ok, cool, thanks Garrett. <div><br></div><div>Do you have any experience using gproc for pub/sub?</div><div><br></div><div>Nice email address, btw :-)</div><div><br></div><div>Andrew<br><br><div class="gmail_quote">On Fri, May 18, 2012 at 2:30 PM, Garrett Smith <span dir="ltr"><<a href="mailto:g@rre.tt" target="_blank">g@rre.tt</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Thu, May 17, 2012 at 7:14 PM, Andrew Berman <<a href="mailto:rexxe98@gmail.com">rexxe98@gmail.com</a>> wrote:<br>
</div><div><div class="h5">> Hello,<br>
><br>
> I am trying to set up a pub-sub type situation where I have a function<br>
> called which does some stuff and at the end broadcasts a message. I would<br>
> like for multiple handlers to be able to receive that message and do<br>
> something with it. I'm currently using gen_event, but have been reading<br>
> about gproc as well. Can anyone explain why they'd use one over the other<br>
> for pub/sub?<br>
<br>
</div></div>gen_event is useful when you need to support "pluggable" functionality<br>
and you can't tolerate the overhead of Erlang message passing.<br>
<br>
gen_event runs all handlers in a single process. If one of those<br>
subscribers misbehaves, it's unceremoniously removed.<br>
<br>
To give you an idea:<br>
<br>
Eshell V5.8.5 (abort with ^G)<br>
1> error_logger:info_msg("say hello").<br>
<br>
=INFO REPORT==== 18-May-2012::16:13:25 ===<br>
say hellook<br>
2> error_logger:info_msg([{"say", "goodbye"}]).<br>
ok<br>
3> error_logger:info_msg("anyone?").<br>
ok<br>
<br>
error_logger uses gen_event -- tty support is a handler. Bugs, as the<br>
one above, result in silent changes to your program!<br>
<br>
While you can use add_handler_sup to get notification of a handler<br>
delete, I find the whole facility easy to get wrong and so I avoid it.<br>
<br>
But it's not hard to create a publisher in yourself. e2 [1] has<br>
something you can steal from, or just use:<br>
<br>
<a href="https://github.com/gar1t/e2/blob/master/src/e2_publisher.erl" target="_blank">https://github.com/gar1t/e2/blob/master/src/e2_publisher.erl</a><br>
<br>
Here's a silly example of a "broker" that decorates published messages<br>
with some tag:<br>
<br>
<a href="https://github.com/gar1t/e2/blob/master/examples/pubsub/src/broker.erl" target="_blank">https://github.com/gar1t/e2/blob/master/examples/pubsub/src/broker.erl</a><br>
<br>
The advantage of pubsub using Erlang messages over gen_event is that<br>
you can implement more natural supervision -- and you're not going to<br>
get silent, mysterious breakage. The price is that you're sending<br>
Erlang messages. If you can't afford that (e.g. perhaps high<br>
throughput logging) gen_event is better.<br>
<br>
Garrett<br>
<br>
[1] <a href="http://e2project.org/" target="_blank">http://e2project.org/</a><br>
</blockquote></div><br></div>