View Source ebus (ebus v0.3.0)
ebus` interface. This module also works as a wrapper on top of `ebus_ps module.See also: ebus_ps.
Link to this section Summary
Functions
ebus server name: ebus_ps.Equivalent to dispatch(Topic, Message, []).
Sends a message only to one subscriber handler of the Topic.
Equivalent to local_subscribers(server(), Topic).
Same as subscribers/2 but only local subscribers handlers for the given Topic are returned.
Equivalent to local_topics(server()).
Same as topics/1 but only local topics are returned.
Equivalent to pub(server(), Topic, Message).
Sends a message to all subscribers of a topic.
Same as pub/3 but message is not sent to FromHandler.
ebus server name.ebus application.ebus application.Equivalent to sub(server(), Handler, Topic).
Equivalent to sub(Server, Handler, Topic, []).
Subscribes the Handler given Topic.
Equivalent to subscribers(server(), Topic).
Returns a set of all subscribers handlers (local and global) for the given Topic.
Equivalent to topics(server()).
Returns the list of all topics (local and global) in use. This is an expensive and private operation.
Equivalent to unsub(server(), Handler, Topic).
Unsubscribes the given Handler from the Topic.
Link to this section Types
-type dispatch_fun() :: fun(([term()]) -> term()).
-type dispatch_opt() :: {scope, local | global} | {dispatch_fun, dispatch_fun()}.
-type dispatch_opts() :: [dispatch_opt()].
-type handler() :: pid().
-type options() :: ebus_ps_local:options().
-type topic() :: iodata().
Link to this section Functions
-spec default_ps_server() -> ebus_ps.
ebus server name: ebus_ps.
Equivalent to dispatch(Topic, Message, []).
Equivalent to dispatch(server(), Topic, Message, Opts).
-spec dispatch(atom(), topic(), term(), dispatch_opts()) -> ok.
Sends a message only to one subscriber handler of the Topic.
Server: The registered server name or pid.Topic: The string topic, for example<<"users:123">>.Message: Any erlang term.Opts: The optional list of options. See below.
{scope, local | global}: define if the message must be delivered to a local or global (any) process. Default islocal.{dispatch_fun, dispatch_fun()}: allows to pass a function to choose a subscriber from the current subscribers handlers to a topic.
Examples:
> ebus:dispatch("bar", #{topic => "foo", payload => "hi"}).
ok
> ebus:dispatch("bar", #{topic => "foo", payload => "hi"}, []).
ok
> ebus:dispatch(ebus_ps, "bar", "my message",
[{scope, global}, {dispatch_fun, fun([H | _]) -> H end}]).
ok
Equivalent to local_subscribers(server(), Topic).
-spec local_subscribers(atom(), topic()) -> [pid()].
Same as subscribers/2 but only local subscribers handlers for the given Topic are returned.
Example:
> ebus:local_subscribers(ebus_ps, <<"foo">>).
[<0.48.0>, <0.49.0>]
Equivalent to local_topics(server()).
-spec local_topics(atom()) -> [binary()].
Same as topics/1 but only local topics are returned.
Example:
> ebus:local_topics().
[<<"foo">>, <<"bar">>]
> ebus:local_topics(ebus_ps).
[<<"foo">>, <<"bar">>]
Equivalent to pub(server(), Topic, Message).
-spec pub(atom(), topic(), term()) -> ok | {error, term()}.
Sends a message to all subscribers of a topic.
Server: The registered server name or pid.Topic: The string topic, for example<<"users:123">>.Message: Any erlang term.
Examples:
> ebus:pub("bar", #{topic => "foo", payload => "hi"}).
ok
> ebus:pub(ebus_ps, "bar", #{topic => "foo", payload => "hi"}).
ok
Equivalent to pub_from(server(), From, Topic, Message).
Same as pub/3 but message is not sent to FromHandler.
Examples:
> ebus:pub_from(self(), "foo", <<"message">>).
ok
> ebus:pub_from(ebus_ps, self(), "foo", <<"message">>).
ok
-spec server() -> atom().
ebus server name.
-spec start() -> {ok, _} | {error, term()}.
ebus application.
-spec stop() -> ok | {error, term()}.
ebus application.
Equivalent to sub(server(), Handler, Topic).
Equivalent to sub(Server, Handler, Topic, []).
Subscribes the Handler given Topic.
Server: The Pid registered name of the server.Handler: The subscriber pid to receive pubsub messages.Topic: The topic to subscribe to, ie:"users:123".Opts: The optional list of options. See below.
{link, _}: links the subscriber to the pubsub adapter.{fastlane, ebus_ps_local:fastlane()}: Provides a fastlane path for the broadcasts forbroadcast()events. The fastlane process is notified of a cached message instead of the normal subscriber. Fastlane handlers must implementfastlane/1callbacks which accepts abroadcast()struct and returns a fastlaned format for the handler.
Examples:
> ebus:sub(self(), <<"foo">>).
ok
> ebus:sub(ebus_ps, self(), <<"foo">>).
ok
> ebus:sub(ebus_ps, self(), <<"foo">>, []).
ok
> ebus:sub(ebus_ps, self(), <<"foo">>,
[{fastlane, {FastPid, my_serializer, [<<"event1">>]}]).
ok
Equivalent to subscribers(server(), Topic).
-spec subscribers(atom(), topic()) -> [pid()].
Returns a set of all subscribers handlers (local and global) for the given Topic.
Server: The registered server name or pid.Topic: The string topic, for example<<"users:123">>.
Example:
> ebus:subscribers(ebus_ps, <<"foo">>).
[<0.48.0>, <0.49.0>]
Equivalent to topics(server()).
-spec topics(atom()) -> [binary()].
Returns the list of all topics (local and global) in use. This is an expensive and private operation.
This is an expensive operation. DO NOT USE IT IN PROD
Example:
> ebus:topics().
[<<"foo">>, <<"bar">>]
> ebus:topics(ebus_ps).
[<<"foo">>, <<"bar">>]
Equivalent to unsub(server(), Handler, Topic).
Unsubscribes the given Handler from the Topic.
Server: The registered server name or pid.Handler: The subscriber pid.Topic: The string topic, for example<<"users:123">>.
Example:
> ebus:unsub(self(), <<"foo">>).
ok
> ebus:unsub(ebus_ps, self(), <<"foo">>).
ok