PlanB.EventBus (planb v0.1.3)

Module EventBus

This module is created as a GenServer. To use it just call start_link/1, then create a channel and register subscribers to it.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

health_check/0

Example for checking if the gen server is currently running

Callback implementation for GenServer.init/1.

lookup/1

Example calling lookup function to retrieve the registered channel.

publish/2

Example publishing an event over the channel. The message is published async over the channel, so do not expect any return message.

register/2

Example registering a gen server in the event bus. There are two params that needs passing in

start_link/1

Examples

Example starting the EventBus with no registered modules

stop_server/0

Example stopping the EventBus gen server after staring it

Link to this section Functions

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Specs

health_check() :: :fail | :ok

health_check/0

Example for checking if the gen server is currently running

iex>PlanB.EventBus.health_check
:ok
iex>PlanB.EventBus.health_check
:fail

Specs

init(any()) :: {:ok, any()}

Callback implementation for GenServer.init/1.

Specs

lookup(binary()) :: any()

lookup/1

Example calling lookup function to retrieve the registered channel.

iex>PlanB.EventBus.lookup("my_example_channel")
{"my_example_channel", [{YourApplication.ServerA, #PID<0.210.0>}]}

Example calling lookup if the GenServer process that is registered in the channel is not alive.

iex>PlanB.EventBus.lookup("my_example_channel")
{"my_example_channel", [{YourApplication.ServerA, nil}]}
Link to this function

publish(name, payload)

Specs

publish(binary(), any()) :: any()

publish/2

Example publishing an event over the channel. The message is published async over the channel, so do not expect any return message.

iex>PlanB.EventBus.publish("my_example_channel", {:count, 100})
:ok
Link to this function

register(name, module)

Specs

register(binary(), Module) :: any()

register/2

Example registering a gen server in the event bus. There are two params that needs passing in:

  • name: The name of the channel. If the channel is already created, than add the module to that channel, otherwise create a new channel.
  • module: Pass in the module. Make sure the gen server of the module was started and registered under the module name.

Example:

iex>fake_server = GenServer.start_link(MyApp.MyModule, %{}, name: MyApp.MyModule)
{:ok, #PID<0.210.0>}
iex>PlanB.EventBus.register("channel_name", MyApp.MyModule)
:ok
Link to this function

start_link(args)

Specs

start_link(any()) :: :ignore | {:error, any()} | {:ok, pid()}

Examples

Example starting the EventBus with no registered modules

iex>PlanB.EventBus.start_link([%{}])
{:ok, #PID<0.210.0>}

Example stating the EventBus with one channel and two registered modules

iex>init_state = %{"channel_a" => [YourApplication.ServerA, YourApplication.ServerB]}
%{"channel_a" => [YourApplication.ServerA, YourApplication.ServerB]}
iex>PlanB.EventBus.start_link([init_state])
{:ok, #PID<0.210.0>}

Example stating the EventBus with two channel each with one registerd module

iex>init_state = %{"channel_a" => [YourApplication.ServerA], "channel_b" => [YourApplication.ServerB]}
%{
    "channel_a" => [YourApplication.ServerA],
    "channel_b" => [YourApplication.ServerB]
}
iex>PlanB.EventBus.start_link([init_state])
{:ok, #PID<0.210.0>}

Specs

stop_server() :: :ok

stop_server/0

Example stopping the EventBus gen server after staring it

iex>PlanB.EventBus.stop_server
:ok