onyx-eda
Onyx::EDA
An Event-Driven Architecture framework to build reactive apps.
Supporters β€οΈ
Thanks to all my patrons, I can continue working on beautiful Open Source Software! π
Lauri Jutila, Alexander Maslov, Dainel Vera
You can become a patron too in exchange of prioritized support and other perks
About π
Onyx::EDA is an Event-Driven Architecture framework. It allows to emit certain events and subscribe to them.
It has several channels implemented:
- In-memory channel
- Redis channel (working on Redis streams)
Installation π₯
Add this to your application's shard.yml
:
dependencies:
onyx-eda:
github: onyxframework/eda
version: ~> 0.2.0
This shard follows Semantic Versioning v2.0.0, so check releases and change the version
accordingly. Please visit github.com/crystal-lang/shards to know more about Crystal shards.
Usage π»
First of all, you need to create a channel:
require "onyx-eda"
channel = Onyx::EDA::Channel.new
# or
#
require "onyx-eda"
require "onyx-eda/channel/redis"
channel = Onyx::EDA::Channel::Redis.new("redis://localhost:6379")
Then define events to emit:
struct MyEvent
include Onyx::EDA::Event
getter foo
def initialize(@foo : String)
end
end
Subscribe an object to the event:
# Top-level subscription
channel.subscribe(Object, MyEvent) do |event|
pp event.foo
end
# Object-level subscriptions
#
class Notifier
def initialize(channel)
channel.subscribe(self, MyEvent) do |event|
pp event.foo
end
end
end
notifier = Notifier.new(channel)
Then emit the event:
channel.emit(MyEvent.new("bar"))
All subscribers will asynchronously notified of the new event. You can then unsubscribe:
channel.unsubscribe(notifier)
Using with Onyx top-level macros
Onyx shard has convenient macros to reduce boilerplate code. Once "onyx/eda"
is required, a singleton Onyx.channel
is defined. You can then specify which channel to use, for example, Onyx.channel(:redis)
.
require "onyx/eda"
struct MyEvent
# ditto
end
Onyx.channel(:redis)
Onyx.subscribe(Object, MyEvent) do |event|
# ditto
end
Onyx.emit(MyEvent.new("bar"))
# or
Onyx.channel.emit(MyEvent.new("bar"))
sleep(0.1)
Onyx.unsubscibe(Object)
Community πͺ
There are multiple places to talk about this particular shard and about other ones as well:
- Onyx::EDA Gitter chat
- Onyx Framework Gitter community
- Vlad Faust Gitter community
- Onyx Framework Twitter
- Onyx Framework Telegram channel
Support β€οΈ
This shard is maintained by me, Vlad Faust, a passionate developer with years of programming and product experience. I love creating Open-Source and I want to be able to work full-time on Open-Source projects.
I will do my best to answer your questions in the free communication channels above, but if you want prioritized support, then please consider becoming my patron. Your issues will be labeled with your patronage status, and if you have a sponsor tier, then you and your team be able to communicate with me in private or semi-private channels such as e-mail and Twist. There are other perks to consider, so please, don't hesistate to check my Patreon page:
You could also help me a lot if you leave a star to this GitHub repository and spread the world about Crystal and Onyx! π£
Contributing
- Fork it ( https://github.com/onyxframework/eda/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'feat: some feature') using Angular style commits
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- Vlad Faust - creator and maintainer
Licensing
This software is licensed under MIT License.