@emerson-evolution-gym/events
v0.1.0
Published
Event bus and event handling utilities for Evolution Gym
Maintainers
Readme
@evolution-gym/events
Event bus and event handling utilities for Evolution Gym microservices using RabbitMQ.
Installation
npm install @evolution-gym/eventsUsage
Initialize Event Bus
import { RabbitMQEventBus } from '@evolution-gym/events';
const eventBus = new RabbitMQEventBus({
url: process.env.RABBITMQ_URL || 'amqp://localhost',
exchange: 'evolution-gym-events',
queuePrefix: 'auth-service-'
});
await eventBus.connect();Publish Events
import { EventFactory } from '@evolution-gym/events';
import { EventType } from '@evolution-gym/types';
// Create and publish event
const event = EventFactory.createMemberEvent(
EventType.MEMBER_CREATED,
memberId,
tenantId,
'member-service',
{ name: 'John Doe' }
);
await eventBus.publish(event);Subscribe to Events
import { EventHandler } from '@evolution-gym/types';
import { EventType } from '@evolution-gym/types';
class MemberCreatedHandler implements EventHandler {
eventType = EventType.MEMBER_CREATED;
async handle(event) {
console.log('Member created:', event.data);
// Handle the event
}
}
const handler = new MemberCreatedHandler();
await eventBus.subscribe(EventType.MEMBER_CREATED, handler);Event Factory Helpers
import { EventFactory } from '@evolution-gym/events';
import { EventType } from '@evolution-gym/types';
// User events
const userEvent = EventFactory.createUserEvent(
EventType.USER_CREATED,
userId,
'auth-service'
);
// Member events
const memberEvent = EventFactory.createMemberEvent(
EventType.MEMBER_CHECK_IN,
memberId,
tenantId,
'member-service',
{ locationId: 'gym-1' }
);
// Payment events
const paymentEvent = EventFactory.createPaymentEvent(
EventType.PAYMENT_APPROVED,
paymentId,
userId,
'payment-service',
{ amount: 99.90 }
);Graceful Shutdown
process.on('SIGTERM', async () => {
await eventBus.close();
process.exit(0);
});Configuration
Environment variables:
RABBITMQ_URL- RabbitMQ connection URL
Event Flow
- Service publishes event to exchange
- Exchange routes event to queues based on routing key
- Consumers process events from their queues
- Acknowledgment ensures reliable delivery
Best Practices
- Always handle errors in event handlers
- Use idempotency keys to prevent duplicate processing
- Keep events small - include only necessary data
- Version your events for backward compatibility
- Monitor dead letter queues for failed events
License
MIT
