https://github.com/stanipetrosyan/go-eventbus
Event Bus package for Go
https://github.com/stanipetrosyan/go-eventbus
eventbus go golang hacktoberfest pubsub
Last synced: 8 days ago
JSON representation
Event Bus package for Go
- Host: GitHub
- URL: https://github.com/stanipetrosyan/go-eventbus
- Owner: stanipetrosyan
- License: mit
- Created: 2022-10-10T19:25:21.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-26T14:34:16.000Z (almost 2 years ago)
- Last Synced: 2024-08-26T16:58:22.446Z (almost 2 years ago)
- Topics: eventbus, go, golang, hacktoberfest, pubsub
- Language: Go
- Homepage:
- Size: 74.2 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - go-eventbus - Simple Event Bus package for Go. (Messaging / Search and Analytic Databases)
- fucking-awesome-go - go-eventbus - Simple Event Bus package for Go. (Messaging / Search and Analytic Databases)
- awesome-go-with-stars - go-eventbus - 05-05 | (Messaging / Search and Analytic Databases)
- awesome-go - go-eventbus - Simple Event Bus package for Go. (Messaging / Search and Analytic Databases)
- awesome-go - go-eventbus - Simple Event Bus package for Go. (Messaging / Search and Analytic Databases)
- awesome-go-plus - go-eventbus - Simple Event Bus package for Go.  (Messaging / Search and Analytic Databases)
- awesome-go-cn - go-eventbus - eventbus) (消息 / 检索及分析资料库)
- awesome-go-cn - go-eventbus - eventbus) (消息 / 检索及分析资料库)
README
[](https://goreportcard.com/report/github.com/stanipetrosyan/go-eventbus)
[](https://codecov.io/gh/stanipetrosyan/go-eventbus)
[](https://pkg.go.dev/github.com/stanipetrosyan/go-eventbus)

# EventBus for Golang
## Description
This is a simple implementation of an event bus in golang. Actually support follwing pattern:
- [x] publish/subscribe
- [x] request/response
## Get Started
To start use eventbus in your project, you can run the following command.
```
go get github.com/stanipetrosyan/go-eventbus
```
And import
``` go
import (
goeventbus "github.com/stanipetrosyan/go-eventbus"
)
```
## Publish/Subscribe
Simple example of publish/subscribe pattern.
```go
eventbus = goeventbus.NewEventBus()
address := "topic"
options := goeventbus.NewMessageHeadersBuilder().SetHeader("header", "value").Build()
message := goeventbus.NewMessageBuilder().SetPayload("Hi Topic").SetHeaders(options).Build()
eventbus.Channel(address).Subscriber().Listen(func(dc goeventbus.Context) {
fmt.Printf("Message %s\n", dc.Result().Data)
})
eventbus.Channel(address).Publisher().Publish(message)
```
## Request/Response
Simple example of request/response pattern.
```go
eventbus = goeventbus.NewEventBus()
address := "topic"
message := goeventbus.NewMessageBuilder().SetPayload("Hi Topic").Build()
eventbus.Channel(address).Subscriber().Listen(func(context goeventbus.Context) {
fmt.Printf("Message %s\n", context.Result().Extract())
context.Reply("Hello from subscriber")
})
eventbus.Channel(address).Publisher().Request(message, func(context goeventbus.Context) {
fmt.Printf("Message %s\n", context.Result().Extract())
})
```
## Message
For publishing, you need to create a Message object using this method.
```go
message := goeventbus.NewMessageBuilder().SetPayload("Hi Topic").SetHeaders(options).Build()
```
Each message can have some options:
```go
options := goeventbus.NewMessageHeadersBuilder().SetHeader("header", "value").Build()
message := goeventbus.NewMessageBuilder().setHeaders(options).Build()
eventBus.Channel("address").Publisher().Publish(message)
```
## Processor
A processor works like a middleware, in fact forwards messages only if context Next method is called. The entity works as Subscriber: Listen method accept a callback with context.
The processor intercept message from publisher to subscribers on a specific channel.
```go
eventbus.Channel("topic1").Processor().Listen(func(context goeventbus.Context) {
if context.Result().ExtractHeaders().Contains("header") {
context.Next()
}
})
```
Inside processor is possible change message to forward using `Map` method:
```go
eventbus.Channel("topic1").Processor().Listen(func(context goeventbus.Context) {
if context.Result().ExtractHeaders().Contains("header") {
newMessage := NewMessageBuilder().SetPayload("new message").Build()
context.Map(newMessage).Next()
}
})
```
## Network Bus
A Network bus create a tcp connection between different services.
NetworkBus is a wrapper of local eventbus.
A simple server/client example is in `examples/networkbus` directory.