Skip to main content
Echoes in Orionjs provide a structured way to implement event-driven architecture and handle inter-service communication. Using the @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

The createEchoEvent() 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
Those errors will be automatically propagated back to the requester.

Type Safety

Using TypeScript with schema inference, you can ensure type safety for your echo handlers:

Best Practices

  1. Organize by Domain: Group related echo handlers in the same controller class.
  2. Leverage Dependency Injection: Use @Inject(() => Service) to access repositories and services.
  3. Keep Methods Focused: Each echo handler should have a clear, single responsibility.
  4. Use Strong Typing: Define parameter and return types with schemas and infer TypeScript types.
  5. Handle Errors Gracefully: Catch and properly categorize errors.
  6. Idempotent Handlers: Design event handlers to be idempotent (safe to process the same event multiple times).
  7. Timeout Configuration: Set appropriate timeouts for requests based on expected execution time.
  8. Security: Use the shared key to secure inter-service communication.
  9. Service Discovery: Keep the service registry updated when adding new services.
  10. Monitoring: Implement proper logging and monitoring for echo handlers.