bevy_event_bus
A Bevy plugin that bridges Bevy's message system with external brokers such as Kafka and Redis. It keeps authoring ergonomics close to standard Bevy patterns while allowing for ingestion, decoding, and delivery of messages to and from a Bevy app.
Features
- Topology-driven registration - declare topics, consumer groups, and event bindings up front via
KafkaTopologyBuilder; the backend applies them automatically during startup. - Multi-decoder pipeline – register multiple decoders per topic and fan decoded payloads out to matching Bevy messages in a single pass.
- Type-safe configuration – reuse strongly typed consumer and producer configs across systems without exposing backend-specific traits in your signatures.
- Rich observability – drain metrics, lag snapshots, and commit statistics surface as Bevy resources/messages ready for diagnostics.
- Extensible architecture – Kafka and Redis ship out of the box; additional brokers can plug in by implementing the backend trait.
Installation
Add the crate to your Cargo.toml and enable the backend features you need:
[]
= { = "0.2", = ["kafka", "redis"] }
Quick start
- Define your Bevy messages. Derive
Message(andEventif you still emit internally), plusSerialize,Deserialize,Clone, andSync— theBusMessagemarker trait is implemented automatically. - Describe your topology. Use the
KafkaTopologyBuilderto declare topics, consumer groups, and which Bevy message types bind to which topics. - Spin up the plugin. Build a
KafkaBackendConfig, pass it toKafkaEventBusBackend, then addEventBusPluginto your app.
use Duration;
use *;
use *;
use ;
;
;
;
Error handling
- Delivery failures surface as
EventBusError<T>messages. Add a Bevy system that readsMessageReader<EventBusError<T>>to react to dropped messages, backend outages, or serialization issues. - Writer error callbacks receive a
BusErrorContextclassified with the sharedEventBusErrorTypeenum, so readers, writers, and delivery failures all share one error vocabulary. - For Kafka-specific acknowledgement workflows, consume
KafkaCommitResultEventand inspectKafkaCommitResultStatsfor aggregate success/failure counts.
Observability & diagnostics
The plugin inserts several resources once the backend activates:
ConsumerMetricstracks queue depths, per-frame drain counts, and idle frame streaks.DrainedTopicMetadataholds the per-topic drained messages that readers consume; consumed prefixes are compacted automatically to keep memory bounded.KafkaLagCacheResource(Kafka only) provides consumer lag estimates for each topic/partition.
Hook these into your diagnostics UI or telemetry exporters to keep an eye on the pipeline.
Testing & performance
- The integration suite under
tests/can launch a temporary Redpanda container when Docker is available. SetKAFKA_BOOTSTRAP_SERVERSto target an existing broker if you prefer to manage infrastructure yourself. - Run
cargo testfor unit coverage and./run_performance_tests.pyto capture throughput metrics. Compare new runs withevent_bus_perf_results.csvto spot regressions; each row records the test name plus send/receive delta percentages so you can gauge improvement or drift at a glance.