rd-event-manager
v1.1.0
Published
A singleton event manager and eventbus package fire-and-forget style for Node.js applications.
Maintainers
Readme
Event Manager
Description:
A Basic EventManager singleton and EventBus class to subscribe Listener and emit events
version:
1.0
- EventManager singleton class
- EventManager Emit and subscribe methods
- EventBus class with subscribe and private getIdGenerator method
1.1.0
- Fix EventBus subscribes handler execution (fire-and-forget)
- Add a new EventBusInterface to allow switch between an EventBus in memory or a KafkaImplementation
EventBus Interface and Kafka adapter
EventBusInterface: A runtime abstract class in
src/EventBusInterface.jsthat documents the expected API for EventBus implementations. Methods:subscribe(eventType, eventHandler),publish(event), optionalconnect()anddisconnect()for async implementations.In-memory EventBus:
src/EventBus.jsimplements the interface and remains the default used by theEventManagersingleton.
KafkaEventBus implementation:
KafkaEventBus is an adapter that uses
kafkajs(optional dependency). To use it:Steps:
- Install
kafkajs:
npm install kafkajs- Install
Create and inject the Kafka bus:
const KafkaEventBus = require('./src/KafkaEventBus'); const EventManagerClass = require('./src/EventManager'); const kafkaBus = new KafkaEventBus({ brokers: ['localhost:9092'] }); const manager = new EventManagerClass(kafkaBus); // or modify the exported singleton if desired
Notes:
KafkaEventBus.publishis async and returns the producer send Promise.KafkaEventBus.subscribestarts a consumer and callseventHandler.handle(event)for incoming messages.- The current adapter is a starter implementation; for production you should refine consumer group/topic management and error handling.
