PubSubAdapter trait defines the interface for pub/sub messaging in III. PubSub adapters enable event-driven architectures by allowing functions to publish and subscribe to topics.
Trait Definition
/workspace/source/src/modules/pubsub/mod.rs:21
Methods
publish
topic- The topic name to publish topubsub_data- JSON value containing the event data
subscribe
topic- The topic name to subscribe toid- Unique subscription identifierfunction_id- The function to invoke when events are published
- When an event is published to the topic, the specified function is called with the event data
- Multiple subscriptions to the same topic are supported
- Subscriptions persist until explicitly unsubscribed
unsubscribe
topic- The topic name to unsubscribe fromid- The subscription identifier to remove
- Stops the function from receiving events for this topic
- If this is the last subscription, resources may be cleaned up
Available Adapters
RedisAdapter
Redis-based pub/sub for distributed event messaging across multiple engine instances.- Distributed messaging across engine instances
- Persistent connections with automatic reconnection
- Asynchronous event handling
- Per-topic subscription tasks
/workspace/source/src/modules/pubsub/adapters/redis_adapter.rs
LocalAdapter
In-memory pub/sub for single-instance deployments and development.- Zero external dependencies
- Low latency event delivery
- Perfect for development and testing
- Events only delivered within the same process
/workspace/source/src/modules/pubsub/adapters/local_adapter.rs
Example Implementation
Usage Example
Defining a function that subscribes to events:Implementation Notes
Error Handling
Thepublish method doesn’t return errors. Implementations should:
- Log errors internally using
tracing::error! - Continue processing other subscribers if one fails
- Not block the publisher on delivery failures
Concurrency
Subscriber functions are typically invoked concurrently:- Use
tokio::spawnto invoke functions asynchronously - Multiple events can be processed simultaneously
- Consider rate limiting for high-volume topics
Cleanup
Implementations should:- Track active subscriptions and spawned tasks
- Abort tasks when unsubscribing
- Clean up resources when topics have no subscribers
- Implement proper shutdown on adapter destruction