View Source ebus (ebus v0.3.0)

Main 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

Returns default ebus server name: ebus_ps.

Sends a message only to one subscriber handler of the Topic.

Same as subscribers/2 but only local subscribers handlers for the given Topic are returned.

Same as topics/1 but only local topics are returned.

Sends a message to all subscribers of a topic.

Same as pub/3 but message is not sent to FromHandler.

Returns the registered ebus server name.
Starts ebus application.
Stops ebus application.

Subscribes the Handler given 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.

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.
Returns default ebus server name: ebus_ps.
Link to this function

dispatch(Topic, Message)

View Source

Equivalent to dispatch(Topic, Message, []).

Link to this function

dispatch(Topic, Message, Opts)

View Source

Equivalent to dispatch(server(), Topic, Message, Opts).

Link to this function

dispatch(Server, Topic, Message, Opts)

View Source
-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.
Options:
  • {scope, local | global}: define if the message must be delivered to a local or global (any) process. Default is local.
  • {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
Link to this function

local_subscribers(Topic)

View Source

Equivalent to local_subscribers(server(), Topic).

Link to this function

local_subscribers(Server, Topic)

View Source
-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).

Link to this function

pub(Server, Topic, Message)

View Source
-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
Link to this function

pub_from(From, Topic, Message)

View Source

Equivalent to pub_from(server(), From, Topic, Message).

Link to this function

pub_from(Server, FromHandler, Topic, Message)

View Source
-spec pub_from(atom(), handler(), topic(), term()) -> ok | {error, term()}.

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().
Returns the registered ebus server name.
-spec start() -> {ok, _} | {error, term()}.
Starts ebus application.
Link to this function

start(StartType, StartArgs)

View Source
-spec stop() -> ok | {error, term()}.
Stops ebus application.

Equivalent to sub(server(), Handler, Topic).

Link to this function

sub(Server, Handler, Topic)

View Source

Equivalent to sub(Server, Handler, Topic, []).

Link to this function

sub(Server, Handler, Topic, Opts)

View Source
-spec sub(atom(), handler(), topic(), options()) -> ok | {error, term()}.

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.
Options:
  • {link, _}: links the subscriber to the pubsub adapter.
  • {fastlane, ebus_ps_local:fastlane()}: Provides a fastlane path for the broadcasts for broadcast() events. The fastlane process is notified of a cached message instead of the normal subscriber. Fastlane handlers must implement fastlane/1 callbacks which accepts a broadcast() 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).

Link to this function

subscribers(Server, Topic)

View Source
-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).

Link to this function

unsub(Server, Handler, Topic)

View Source
-spec unsub(atom(), handler(), topic()) -> ok | {error, term()}.

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