https://github.com/steinfletcher/bus
:bus: Simple message bus that supports synchronous and asynchronous message processing
https://github.com/steinfletcher/bus
async asynchronous golang message-bus queue synchronous
Last synced: about 2 months ago
JSON representation
:bus: Simple message bus that supports synchronous and asynchronous message processing
- Host: GitHub
- URL: https://github.com/steinfletcher/bus
- Owner: steinfletcher
- Created: 2021-07-27T21:00:02.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-29T20:37:15.000Z (almost 5 years ago)
- Last Synced: 2025-06-01T10:27:16.804Z (about 1 year ago)
- Topics: async, asynchronous, golang, message-bus, queue, synchronous
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# bus
Simple message bus that supports synchronous and asynchronous message processing. It is intended to be used within a single application to help implement loosely coupled components.
```go
msgBus := bus.New()
```
## Publish
Publish a message to the bus.
```go
msgBus.Publish(context.Background(), Message{Content: "hello"})
```
## Subscribe
Subscribe to a message by its type.
```go
msgBus.Subscribe(func(ctx context.Context, message Message) error {
message.Result = "1234"
return nil
})
```
The subscriber is run synchronously. A common pattern is to mutate the message in the subscriber, allowing the publisher to access the return value. For example
```go
b := bus.New()
b.Subscribe(func(ctx context.Context, query *GetUserQuery) {
query.Result = UserResult{
Name: "Jan",
Email: "jan@hey.com",
}
})
query := GetUserQuery{ID: "1234"}
b.Publish(context.Background(), &query)
fmt.Println(query.Result.Name) // prints Jan
```
## SubscribeAsync
Subscribe to a message by its type. The handler is invoked in a separate go routine and doesn't block the calling go routine.
```go
msgBus.SubscribeAsync(func(ctx context.Context, message Message) {
fmt.Println(message.Content)
})
```