S42-Core EventsDomain Class
EventsDomain - s42-core
The EventsDomain
class is a core utility of the s42-core
package, designed to facilitate event-driven communication between microservices or cells in a distributed system. It enables seamless publishing, listening, and routing of events via Redis, ensuring efficient and decoupled interactions between independent services.
Purpose
The EventsDomain
class is particularly useful in scenarios where microservices or cells need to interact indirectly through events. For example:
- User Registration and Email Sending: A user registration service emits an event when a new user is created. The email service listens to this event to send a welcome email.
- Order Processing and Notification: An order processing service emits events that trigger notifications to customers via a separate notification service.
- Real-Time Updates: Events can be used to update UI clients or trigger specific actions in other services.
Installation
Install the s42-core
package:
Features
- Event Broadcasting: Publish events that can be consumed by other services.
- Listener Registration: Easily register listeners for specific events.
- Dynamic Event Routing: Supports routing of events to specific instances.
- Redis Integration: Uses Redis for reliable and efficient communication.
- Singleton Instance: Ensures a single instance of
EventsDomain
across the application.
Usage
Setting Up EventsDomain
Registering an Event Listener
You can listen to specific events and trigger a callback when the event occurs:
Emitting Events
To emit an event for other services to consume:
Closing EventsDomain
When shutting down the service, clean up resources:
Full Example
Scenario: User Registration and Email Service
User Registration Service
Email Service
Methods
getInstance(redisInstance: RedisClient, uuid: string): EventsDomain
Returns a singleton instance of EventsDomain
.
redisInstance
(RedisClient): The Redis client instance for communication.uuid
(string): A unique identifier for the process.
listenEvent<TypePayload>(eventName: string, callback: (payload: TypePayload) => void): void
Registers a listener for a specific event.
eventName
(string): The name of the event to listen for.callback
(function): The function to execute when the event is received.
emitEvent(eventName: string, payload: object): boolean
Emits an event to be consumed by other services.
eventName
(string): The name of the event to emit.payload
(object): The data to send with the event.
getAllRegisteredEvents(): Record<string, TypeEvent>
Returns all registered events.
close(): void
Stops broadcasting events and cleans up Redis connections.
Advantages
- Decoupling: Services can communicate without direct dependencies.
- Scalability: Easily handle multiple instances and distribute events efficiently.
- Reliability: Uses Redis for robust messaging between services.
- Simplicity: Provides a straightforward API for managing events.
License
This project is licensed under the MIT License. See the LICENSE file for more details.