@Echoes()
and @EchoRequest()
or @EchoEvent()
decorators, you can easily create handlers for both synchronous requests and asynchronous events.
Creating Echo Controllers
An echoes controller is a class decorated with@Echoes()
that contains methods decorated with @EchoRequest()
or @EchoEvent()
:
Echo Request Handlers
Use the@EchoRequest()
decorator with the createEchoRequest()
function to define methods that handle synchronous requests from other services:
Echo Event Handlers
Use the@EchoEvent()
decorator with the createEchoEvent()
function to define methods that process asynchronous events:
Event Decorator Options
ThecreateEchoEvent()
function accepts options similar to createEchoRequest()
:
Making Requests
To make a request to another service:Publishing Events
To publish an event for other services to consume:Starting the Echoes Service
To enable echoes in your application, you need to configure and start the echoes service:Error Handling
Echoes automatically handles errors in request and event handlers:Custom Error Types
Orionjs handles special error types appropriately:- UserError: For expected application errors
- ValidationError: For data validation errors
Type Safety
Using TypeScript with schema inference, you can ensure type safety for your echo handlers:Best Practices
- Organize by Domain: Group related echo handlers in the same controller class.
-
Leverage Dependency Injection: Use
@Inject(() => Service)
to access repositories and services. - Keep Methods Focused: Each echo handler should have a clear, single responsibility.
- Use Strong Typing: Define parameter and return types with schemas and infer TypeScript types.
- Handle Errors Gracefully: Catch and properly categorize errors.
- Idempotent Handlers: Design event handlers to be idempotent (safe to process the same event multiple times).
- Timeout Configuration: Set appropriate timeouts for requests based on expected execution time.
- Security: Use the shared key to secure inter-service communication.
- Service Discovery: Keep the service registry updated when adding new services.
- Monitoring: Implement proper logging and monitoring for echo handlers.