This package offers a high-performance, in-process event dispatcher for Go, ideal for decoupling modules and enabling asynchronous event handling. It supports both synchronous and asynchronous processing, focusing on speed and simplicity.
- High Performance: Processes millions of events per second, about 4x to 10x faster than channels.
- Generic: Works with any type implementing the Event interface.
- Asynchronous: Each subscriber runs in its own goroutine, ensuring non-blocking event handling.
Use When:
- ✅ Decoupling modules within a single Go process.
- ✅ Implementing lightweight pub/sub or event-driven patterns.
- ✅ Needing high-throughput, low-latency event dispatching.
- ✅ Preferring a simple, dependency-free solution.
Not For:
- ❌ Inter-process/service communication (use Kafka, NATS, etc.).
- ❌ Event persistence, durability, or advanced routing/filtering.
- ❌ Cross-language/platform scenarios.
- ❌ Event replay, dead-letter queues, or deduplication.
- ❌ Heavy subscribe/unsubscribe churn or massive dynamic subscriber counts.
This repository contains a simple, in-process event dispatcher to be used to decouple internal modules. It provides a generic way to define events, publish and subscribe to them.
For convenience, this package provides a default global dispatcher that can be used with On() and Emit() package-level functions.
When publishing events, you can create a Dispatcher which is then used as a target of generic event.Publish[T]() and event.Subscribe[T]() functions to publish and subscribe to various event types respectively.
It should output something along these lines, where order is not guaranteed given that both subscribers are processing messages asyncrhonously.
Please note that the benchmarks are run on a 13th Gen Intel(R) Core(TM) i7-13700K CPU, and results may vary based on the machine and environment. This one demonstrates the publishing throughput of the event dispatcher, at different number of event types and subscribers.
This project is licensed under the MIT License - see the LICENSE file for details.
.png)

