erlang - pub/sub with gproc - what is module? -
reading gproc docs, looks key
used routed messages different processes, can't figure out how pub/sub example works in docs, seems using different keys registering , sending messages.
in gproc docs, provide following example of implementing pub/sub:
subscribe(event) -> gproc:reg({p, l, {?module, event}}). publish(event, data) -> gproc:send({p, l, {?module, event}}, {?module, event, data}).
the gproc:send
2nd argument: {?module, event, data}
, wouldn't make message different based on module sending event?
so example if subscribe event of type foo_bar
module1:
pub_sub:subscribe(foo_bar).
and publish event module2:
pub_sub:publish(foo_bar, {color, "blue"}).
the calls gproc first call be:
gproc:reg({p, l, {module1, foo_bar}}).
while second:
gproc:send({p, l, {module2, foo_bar}}, {module2, foo_bar, {color, "blue}}).
so seems keys different: {p, l, {module1, foo_bar}}
, {p, l, {module2, foo_bar}}
, , module1 never receive message.
or missing something?
p.s.: there different syntax here, still see same problem:
subscribe(eventtype) -> %% gproc notation: {p, l, name} means {(p)roperty, (l)ocal, name} gproc:reg({p, l, {?module, eventtype}}). notify(eventtype, msg) -> key = {?module, eventtype}, gproc:send({p, l, key}, {self(), key, msg}).
nevermind. key same, because ?module
macro expands module defined in!
so {p, l, {pub_sub, foo_bar}}
both calls.
references: this post useful in understanding gproc.
Comments
Post a Comment