shared-kafka-module
v1.4.0
Published
Shared Kafka module for notification services
Readme
Shared Kafka Module
A shared TypeScript module for Kafka messaging in the notification system.
Features
- Type-safe Kafka producer and consumer
- Predefined notification topics and message types
- Error handling and logging
- Async/await support
Installation
npm install shared-kafka-moduleUsage
Producer Example
import { KafkaProducer, KafkaTopics, EmailNotification } from 'shared-kafka-module';
const producer = new KafkaProducer('my-app', ['localhost:9092']);
await producer.connect();
const emailNotification: EmailNotification = {
to: '[email protected]',
subject: 'Welcome',
body: 'Welcome to our service!'
};
await producer.sendMessage(KafkaTopics.EMAIL_NOTIFICATION, emailNotification);Consumer Example
import { KafkaConsumer, KafkaTopics, EmailNotification } from 'shared-kafka-module';
const consumer = new KafkaConsumer('my-app', ['localhost:9092'], 'email-group');
await consumer.connect();
await consumer.subscribe<EmailNotification>(
KafkaTopics.EMAIL_NOTIFICATION,
async (message) => {
console.log('Received email notification:', message);
// Process the message
}
);Configuration
The module requires the following configuration:
clientId: Unique identifier for the Kafka clientbrokers: Array of Kafka broker addressesgroupId: Consumer group ID (for consumers only)
Error Handling
The module includes comprehensive error handling:
- Connection errors are logged and thrown
- Message processing errors are caught and logged
- Failed messages can be handled with custom logic
Types
The module includes TypeScript types for:
- Email notifications
- SMS notifications
- Kafka topics
Contributing
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
