Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

Awesome Lists containing this project

README

        


~ eventbus ~


A go package to send and receive pub-sub messages using channels.


 
 
 



---


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 main

import (
"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.