EventBus
Simple event bus implementation using ETS as an event store.
Installation
The package can be installed by adding event_bus to your list of dependencies in mix.exs:
def deps do
[{:event_bus, "~> 0.4.0"}]
endUsage
Register events in config.exs
In your config.exs you can register events
config :event_bus, events: [:message_received, :another_event_occured]Register events on demand
EventBus.register_event(:my_test_event_occured)Subscribe to the ‘event bus’ with a listener and list of given topics, EventManager will match with Regex
# to catch every event topic
EventBus.subscribe({MyEventListener, [".*"]})
> :ok
# to catch specific topics
EventBus.subscribe({MyEventListener, ["purchase_", "booking_confirmed$", "flight_passed$"]})
> :okUnsubscribe from the ‘event bus’
EventBus.unsubscribe(MyEventListener)
> :okList subscribers
EventBus.subscribers()
> [{MyEventListener, [".*"]}]List subscribers of a specific event
EventBus.subscribers(:hello_received)
> [MyEventListener]Notify all subscribers with any type of data
EventBus.notify({:hello_received, %{message: "Hello"}})
EventBus.notify({:bye_received, [user_id: 1, goal: "exit"]})Fetch event data
EventBus.fetch_event_data({:bye_received, event_key})Mark as completed on Event Watcher
EventBus.mark_as_completed({MyEventListener, :bye_received, event_key})Mark as skipped on Event Watcher
EventBus.mark_as_skipped({MyEventListener, :bye_received, event_key})Sample Listener Implementation
defmodule MyEventListener do
...
def process({topic, key} = event_shadow) do
GenServer.cast(__MODULE__, event_shadow)
:ok
end
...
def handle_cast({:bye_received, event_key}, state) do
event_data = EventBus.fetch_event_data({:bye_received, event_key})
# do sth with event_data
# update the watcher!
EventBus.mark_as_completed({__MODULE__, :bye_received, event_key})
...
{:noreply, state}
end
def handle_cast({:hello_received, event_key}, state) do
event_data = EventBus.fetch_event_data({:hello_received, event_key})
# do sth with event_data
# update the watcher!
EventBus.mark_as_completed({__MODULE__, :hello_received, event_key})
...
{:noreply, state}
end
def handle_cast({topic, key}, state) do
EventBus.mark_as_skipped({__MODULE__, topic, key})
{:noreply, state}
end
...
endDocumentation
Module docs can be found at https://hexdocs.pm/event_bus. Implementation detail can be found at: https://medium.com/@mustafaturan/event-bus-implementation-s-d2854a9fafd5
Contributing
Issues, Bugs, Documentation, Enhancements
Fork the project
Make your improvements and write your tests(make sure you covered all the cases).
Make a pull request.
License
MIT