https://github.com/sehrgutesoftware/typevent
typevent provides type safe event channels for go/golang
https://github.com/sehrgutesoftware/typevent
eventbus events go golang pubsub redis
Last synced: about 2 months ago
JSON representation
typevent provides type safe event channels for go/golang
- Host: GitHub
- URL: https://github.com/sehrgutesoftware/typevent
- Owner: sehrgutesoftware
- License: mit
- Created: 2023-12-01T08:43:13.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-06T11:18:39.000Z (over 2 years ago)
- Last Synced: 2024-04-28T06:39:01.198Z (about 2 years ago)
- Topics: eventbus, events, go, golang, pubsub, redis
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typevent
[](https://github.com/sehrgutesoftware/typevent/actions/workflows/test.yml)
[](https://pkg.go.dev/github.com/sehrgutesoftware/typevent)
typevent provides type-safe event messaging channels for Go. They can be used to implement Pub/Sub schemes without the need for type assertions, streamlining the application code that uses the event channels.
At the core it consists of a generic `Channel` interface with the following methods:
```go
type Channel[E Event] interface {
// Emit emits an event of type E on the channel.
Emit(E) error
// Subscribe registers a handler for events of type E on the channel.
Subscribe(ctx context.Context, handler Handler[E]) (Subscription, error)
}
```
The package currently provides one implementation of the interface, using [Redis Pub/Sub](https://redis.io/docs/interact/pubsub/) as the backing distribution system. Usage can look as follows:
```go
import (
"context"
"fmt"
redisclient "github.com/redis/go-redis/v9"
"github.com/sehrgutesoftware/typevent/redis"
)
func ExampleNewChannel() {
type event string
// Create a new channel using redis Pub/Sub as the underlying event bus.
client := redisclient.NewClient(&redisclient.Options{Addr: "localhost:6379"})
// conf holds the redis client used by the channel
conf := redis.NewConfig(client)
// This is where we create the channel that can be used to emit and subscribe to events
channel := redis.NewChannel[event](conf, "CHANNEL_NAME")
// Register a subscriber for the channel.
sub, _ := channel.Subscribe(context.Background(), func(ctx context.Context, ev event) error {
fmt.Printf("subscriber says: %s\n", ev)
return nil
})
defer sub.Close()
// Emit an event on the channel.
channel.Emit("Hello World!")
}
```
## Development
### Run Tests
```sh
go test ./...
```