https://github.com/ar3s3ru/gobus
Simple library to use content-based event-driven programming with Golang
https://github.com/ar3s3ru/gobus
eventbus go golang golang-library
Last synced: about 1 year ago
JSON representation
Simple library to use content-based event-driven programming with Golang
- Host: GitHub
- URL: https://github.com/ar3s3ru/gobus
- Owner: ar3s3ru
- License: mit
- Created: 2016-06-20T23:37:38.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-06-21T17:04:33.000Z (about 10 years ago)
- Last Synced: 2025-04-12T13:12:47.637Z (about 1 year ago)
- Topics: eventbus, go, golang, golang-library
- Language: Go
- Size: 34.2 KB
- Stars: 22
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GoBus [](https://travis-ci.org/ar3s3ru/gobus)
Simple asynchronous, content-based event bus for Go.
## Usage
GoBus provides a straightforward implementation for an Event Bus.
Start using the Event Bus this way:
```go
bus := gobus.NewEventBus() // Un-buffered channel
bus := gobus.NewEventBusBuffered(chanSize) // Buffered channel
defer bus.Destruct()
```
GoBus can use a buffered and an un-buffered channel to dispatch events as they arrive.
Always remember to call ```bus.Destruct()``` at the end of the Event Bus usage, as it's needed for
cleanup purposes (closing channels, returning asynchronous goroutines, ...).
#### (Un)Subscription
You can subscribe (and unsubscribe) one or more listeners to the Event Bus like this:
```go
func useString(event string) {
// Do something
}
bus.Subscribe(useString)
bus.UnSubscribe(useString)
// Method chaining
bus.Subscribe(function1).Subscribe(function2).Subscribe(function3).
UnSubscribe(function2).UnSubscribe(function3)
// Variadic arguments
bus.Subscribe(function1, function2, function3)
bus.UnSubscribe(function1, function3, function2)
// Having fun :-)
bus.Subscribe(function1, function2, function3).
UnSubscribe(function1, function2, function3)
```
Listeners must be unary procedures, functions with one input argument and no return arguments.
Listeners are grouped together by their input argument types (meaning that publishing a string will call every string
listeners registered to the bus).
#### Publishing
You can publish events to the Event Bus this way:
```go
bus.Publish("HelloWorld!")
// Method chaining
bus.Publish("HelloWorld!").Publish(12)
```
Events are pushed to a dispatcher channel which will asynchronously calls all the listeners registered
to the event type.
Being asynchronous through goroutines, there are no guarantees on the listeners calling order.
## Contributing
Biggest contribution towards this library is to use it and give us feedback for further improvements and additions.
For direct contributions, branch of from master and do _pull request_.
## License
This library is distributed under the MIT License found in the
[LICENSE](https://github.com/ar3s3ru/gobus/blob/master/LICENSE) file.
Written by Danilo Cianfrone.