bevy_event_bus
A Bevy plugin that connects Bevy's event system to external message brokers like Kafka.
Features
-
Seamless integration with Bevy's event system
- Events are simultaneously sent to both Bevy's internal event system and external message brokers
- Familiar API design following Bevy's conventions (EventBusReader/EventBusWriter)
-
Automatic event registration
- Simply derive
ExternalBusEventon your event types - No manual registration required
- Simply derive
-
Topic-based messaging
- Send and receive events on specific topics (auto-subscribe on first read)
- No manual subscription API required
-
Error handling
- Provides detailed error information for connectivity and serialization issues
- "Try" methods that silently continue on errors for non-critical messaging
-
Backends
- Kafka support (with the "kafka" feature)
- Easily extendable to support other message brokers
Installation
Add to your Cargo.toml:
[]
= "0.1"
With Kafka support:
[]
= { = "0.1", = ["kafka"] }
Usage
Define your events
use *;
use *;
// Define an event - no manual registration needed!
Set up the plugin
use *;
use *;
Send events
// System that sends events
Receive events
// System that receives events
Error Handling
Use try_ methods for non-critical messaging:
// This will silently continue if the message cannot be sent
ev_writer.try_send;
Or handle errors explicitly:
match ev_writer.send
Kafka Backend Configuration
let config = KafkaConfig ;
let kafka_backend = new;
// Add plugin
new.add_plugins;
// Event sending: writer.send(topic, event)
// Event reading: reader.try_read(topic) -> iterator of &T
### Auto-Subscription
Reading from a topic automatically subscribes the consumer to that topic on first use.
### Additional Kafka Config Keys
Use `additional_config` to pass through arbitrary librdkafka properties .
Common keys:
* enable.idempotence=true
* message.timeout.ms=5000
* security.protocol=SSL
* ssl.ca.location=/path/to/ca.pem
* ssl.certificate.location=/path/to/cert.pem
* ssl.key.location=/path/to/key.pem
### Local Development
You can spin up a single-node Kafka automatically in tests. The test harness will:
1. Try to start `bitnami/kafka:latest` exposing 9092 if `KAFKA_BOOTSTRAP_SERVERS` not set.
2. Poll metadata until the broker is ready.
Manual run:
```bash
docker run -d --rm --name bevy_event_bus_kafka -p 9092:9092 \
-e KAFKA_ENABLE_KRAFT=yes \
-e KAFKA_KRAFT_CLUSTER_ID=abcdefghijklmnopqrstuv \
-e KAFKA_CFG_PROCESS_ROLES=broker,controller \
-e KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@localhost:9093 \
-e KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093 \
-e KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 \
-e KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT \
-e KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER \
-e KAFKA_CFG_INTER_BROKER_LISTENER_NAME=PLAINTEXT \
-e KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=true \
bitnami/kafka:latest
Set KAFKA_BOOTSTRAP_SERVERS to override (e.g. in CI):
Testing Notes
Integration test tests/integration/temp.rs uses the docker harness or external broker.
It generates a unique topic name per run to avoid offset collisions.
With Kafka support:
```toml
[dependencies]
bevy_event_bus = { version = "0.1", features = ["kafka"] }
Usage
Define your events
use *;
use *;
// Define an event - no manual registration needed!
Set up the plugin
use *;
use *;
Send events
// System that sends events
Receive events
// System that receives events
Error Handling
Use try_ methods for non-critical messaging:
// This will silently continue if the message cannot be sent
ev_writer.try_send;
Or handle errors explicitly:
match ev_writer.send
Backend Configuration
Kafka
let config = KafkaConfig ;
let kafka_backend = new;
Performance Testing
The library includes comprehensive performance tests to measure throughput and latency under various conditions. See PERFORMANCE_TESTING.md for detailed documentation.
Quick Performance Test
# Run all performance benchmarks
# Run specific test
# Results are automatically saved to event_bus_perf_results.csv
Sample Performance Results
Test: test_message_throughput | Send Rate: 76373 msg/s | Receive Rate: 78000 msg/s | Payload: 100 bytes
Test: test_high_volume_small_messages | Send Rate: 73742 msg/s | Receive Rate: 74083 msg/s | Payload: 20 bytes
Test: test_large_message_throughput | Send Rate: 8234 msg/s | Receive Rate: 8156 msg/s | Payload: 10000 bytes
The performance tests measure:
- Message throughput (messages per second)
- Data throughput (MB/s)
- End-to-end latency
- System stability under load
Performance results are tracked over time with git commit hashes for regression analysis.