event_bus v1.4.0 EventBus

Traceable, extendable and minimalist event bus implementation for Elixir with built-in event store and event observation manager based on ETS

Link to this section Summary

Types

EventBus.Model.Event struct

Event id

Tuple of topic name and event id

Event listener/subscriber/consumer

Listener configuration

List of event listeners/subscribers/consumers

Event listener/subscriber/consumer with config

Tuple of listener and event reference

Tuple of listener and event shadow

Tuple of listener, topic and event id

Tuple of listener and list of topic patterns

Event listener/subscriber/consumer without config

Topic name

List of topic names

Regex pattern to match topic name

List of topic patterns

Functions

Send the event processing completed to the Observation Manager

Send the event processing skipped to the Observation Manager

Send event to all subscribers(listeners)

Register a topic

Is given listener subscribed to the bus for the given topic patterns?

List the subscribers

List the subscribers to the with given topic

Check if topic registered

List all registered topics

Unregister a topic

Unsubscribe from the bus

Link to this section Types

EventBus.Model.Event struct

Link to this type event_id()
event_id() :: String.t() | integer()

Event id

Link to this type event_shadow()
event_shadow() :: {topic(), event_id()}

Tuple of topic name and event id

Event listener/subscriber/consumer

Link to this type listener_config()
listener_config() :: any()

Listener configuration

Link to this type listener_list()
listener_list() :: [listener()]

List of event listeners/subscribers/consumers

Link to this type listener_with_config()
listener_with_config() :: {module(), listener_config()}

Event listener/subscriber/consumer with config

Link to this type listener_with_event_ref()

Tuple of listener and event reference

Link to this type listener_with_event_shadow()
listener_with_event_shadow() :: {listener(), event_shadow()}

Tuple of listener and event shadow

Link to this type listener_with_topic_and_event_id()
listener_with_topic_and_event_id() :: {listener(), topic(), event_id()}

Tuple of listener, topic and event id

Link to this type listener_with_topic_patterns()
listener_with_topic_patterns() :: {listener(), topic_pattern_list()}

Tuple of listener and list of topic patterns

Link to this type listener_without_config()
listener_without_config() :: module()

Event listener/subscriber/consumer without config

Link to this type topic()
topic() :: atom()

Topic name

Link to this type topic_list()
topic_list() :: [topic()]

List of topic names

Link to this type topic_pattern()
topic_pattern() :: String.t()

Regex pattern to match topic name

Link to this type topic_pattern_list()
topic_pattern_list() :: [topic_pattern()]

List of topic patterns

Link to this section Functions

Link to this function fetch_event(event_shadow)
fetch_event(event_shadow()) :: event()

Fetch event

Examples

EventBus.fetch_event({:hello_received, "123"})
%EventBus.Model.Model{}
Link to this function fetch_event_data(event_shadow)
fetch_event_data(event_shadow()) :: any()

Fetch event data

Examples

EventBus.fetch_event_data({:hello_received, "123"})
Link to this function mark_as_completed(listener_with_event_ref)
mark_as_completed(listener_with_event_ref()) :: no_return()

Send the event processing completed to the Observation Manager

Examples

topic        = :hello_received
event_id     = "124"
event_shadow = {topic, event_id}

# For regular listeners
EventBus.mark_as_completed({MyEventListener, event_shadow})

# For configurable listeners you must pass tuple of listener and config
my_config = %{}
listener  = {OtherListener, my_config}

EventBus.mark_as_completed({listener, event_shadow})
:ok
Link to this function mark_as_skipped(listener_with_event_ref)
mark_as_skipped(listener_with_event_ref()) :: no_return()

Send the event processing skipped to the Observation Manager

Examples

EventBus.mark_as_skipped({MyEventListener, {:unmatched_occurred, "124"}})

# For configurable listeners you must pass tuple of listener and config
my_config = %{}
listener  = {OtherListener, my_config}
EventBus.mark_as_skipped({listener, {:unmatched_occurred, "124"}})
:ok
Link to this function notify(event)
notify(event()) :: :ok

Send event to all subscribers(listeners).

Examples

event = %Event{id: 1, topic: :webhook_received,
  data: %{"message" => "Hi all!"}}
EventBus.notify(event)
:ok
Link to this function register_topic(topic)
register_topic(topic()) :: :ok

Register a topic

Examples

EventBus.register_topic(:demo_topic)
:ok
Link to this function subscribe(listener_with_topic_patterns)
subscribe(listener_with_topic_patterns()) :: :ok

Subscribe to the bus.

Examples

EventBus.subscribe({MyEventListener, [".*"]})
:ok

# For configurable listeners you can pass tuple of listener and config
my_config = %{}
EventBus.subscribe({{OtherListener, my_config}, [".*"]})
:ok
Link to this function subscribed?(listener_with_topic_patterns)

Is given listener subscribed to the bus for the given topic patterns?

Examples

EventBus.subscribe({MyEventListener, [".*"]})
:ok

EventBus.subscribed?({MyEventListener, [".*"]})
true

EventBus.subscribed?({MyEventListener, ["some_initialized"]})
false

EventBus.subscribed?({AnothEventListener, [".*"]})
false
Link to this function subscribers()
subscribers() :: listener_list()

List the subscribers.

Examples

EventBus.subscribers()
[MyEventListener]

# One usual and one configured listener with its config
EventBus.subscribers()
[MyEventListener, {OtherListener, %{}}]
Link to this function subscribers(topic)

List the subscribers to the with given topic.

Examples

EventBus.subscribers(:metrics_received)
[MyEventListener]

# One usual and one configured listener with its config
EventBus.subscribers(:metrics_received)
[MyEventListener, {OtherListener, %{}}]
Link to this function topic_exist?(topic)
topic_exist?(topic()) :: boolean()

Check if topic registered.

Examples

EventBus.topic_exist?(:demo_topic)
true
Link to this function topics()
topics() :: topic_list()

List all registered topics.

Examples

EventBus.topics()
[:metrics_summed]
Link to this function unregister_topic(topic)
unregister_topic(topic()) :: :ok

Unregister a topic

Examples

EventBus.unregister_topic(:demo_topic)
:ok
Link to this function unsubscribe(listener)
unsubscribe(listener()) :: :ok

Unsubscribe from the bus.

Examples

EventBus.unsubscribe(MyEventListener)
:ok

# For configurable listeners you must pass tuple of listener and config
my_config = %{}
EventBus.unsubscribe({OtherListener, my_config})
:ok