Overview
Credo uses an event-driven architecture to notify your application about important state changes and actions. TheEventEmitter provides both callback-based and reactive (RxJS Observable) interfaces.
EventEmitter
TheEventEmitter is injected into services and wraps Node.js’s native EventEmitter with Credo-specific functionality.
Event Structure
All events follow theBaseEvent interface:
Listening to Events
Using Callbacks
The simplest way to listen to events:Removing Listeners
Using Observables
For more advanced reactive patterns, use RxJS observables:Filter by Context
In multi-tenant scenarios, filter events by context:Emitting Events
When creating custom services or modules, emit events to notify other parts of the application:Common Event Types
Connection Events
Credential Events
Proof Events
Practical Examples
Auto-Accept Connections
Log All Events
Notify External Service
Event Aggregation with RxJS
Event Lifecycle
TheEventEmitter respects the agent’s lifecycle:
Best Practices
1. Type Your Event Handlers
Always use TypeScript types for better type safety:2. Clean Up Listeners
Remove event listeners when they’re no longer needed:3. Use Observables for Complex Logic
When you need filtering, debouncing, or combining streams, use observables:4. Handle Async Errors in Listeners
5. Filter by Context in Multi-Tenant Apps
Related
Custom Modules
Learn how to emit custom events from modules
Agent API
API reference for the Agent class