Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zekrotja/eventbus
A go package to send and receive pub-sub messages using channels.
https://github.com/zekrotja/eventbus
event-driven eventbus events go go118 gogeneric hacktoberfest pubsub
Last synced: about 1 month ago
JSON representation
A go package to send and receive pub-sub messages using channels.
- Host: GitHub
- URL: https://github.com/zekrotja/eventbus
- Owner: zekroTJA
- License: mit
- Created: 2022-06-28T13:45:51.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-06-28T21:46:56.000Z (over 2 years ago)
- Last Synced: 2024-12-06T18:53:46.097Z (about 2 months ago)
- Topics: event-driven, eventbus, events, go, go118, gogeneric, hacktoberfest, pubsub
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
---
go get -u github.com/zekrotja/eventbus
---
## Intro
This package provides a very simple, generic pub-sub event bus to simplify sending event
messages between services using channels.### Why using channels over callbacks?
Callback are - in my opinion - not a very clean and performant way to perform event driven
development in Go because, in contrast to languages like JavaScript, Go is not an event
driven language. This can lead to blocking publisher routines while waiting for the execution
of callbacks on the side of subscribers. Channels, which are well designed to communicate
between go routines in the first place, are therefore a way better tool to achieve easy, performant
and intuitive communication of events between publishers and subscribers.## Basic Example
```go
package mainimport (
"bufio"
"fmt"
"os""github.com/zekrotja/eventbus"
)func subscriber(name string, bus *eventbus.EventBus[string]) {
c, _ := bus.Subscribe()
for msg := range c {
fmt.Printf("[ -> %s ]: %s\n", name, msg)
}
}func main() {
s := bufio.NewScanner(os.Stdin)
bus := eventbus.New[string]()go subscriber("service1", bus)
go subscriber("service2", bus)bus.SubscribeFunc(func(s string) {
if s == "exit" {
os.Exit(0)
}
})fmt.Println("Publish messages by writing them to the console.\nPress CTRL+C or write 'exit' to exit.")
for s.Scan() {
bus.Publish(s.Text())
}
}
```Further examples can be found in the [examples](examples/) directory. If you want to take alook at a
practical example, feel free to explore my project [Yuri69](https://github.com/zekrotja/yuri69) which
heavily depends on EventBus.