nestjs-zeromq
v1.0.3
Published
Full-featured NestJS wrapper module for ZeroMQ with support for all main socket types
Maintainers
Readme
nestjs-zeromq
A full-featured NestJS wrapper module for ZeroMQ with support for all main socket types (PUB/SUB, REQ/REP, PUSH/PULL, ROUTER/DEALER). Fully integrated with NestJS dependency injection system.
Features
- ✅ All Socket Types: PUB, SUB, REQ, REP, PUSH, PULL, ROUTER, DEALER
- ✅ NestJS Integration: Full dependency injection support
- ✅ Async/Await: All operations support async/await
- ✅ Automatic Reconnection: Auto-reconnect on connection loss
- ✅ Graceful Shutdown: Sockets are properly closed when application shuts down
- ✅ TypeScript Support: Full type safety
- ✅ Error Handling: Comprehensive error handling and logging
- ✅ Flexible Configuration: Individual configuration for each socket
- ✅ Debug Mode: Control debug logging level
- ✅ Property Decorators: Clean injection with
@InjectPublisher,@InjectSubscriber, etc. - ✅ Processor Pattern: BullMQ-like class-based message handling with
@Processdecorator
Installation
npm install nestjs-zeromq zeromqQuick Start
Using Property Decorators
import { Module, OnModuleInit } from '@nestjs/common';
import { ZeroMQModule, InjectPublisher, InjectSubscriber } from 'nestjs-zeromq';
import { PublisherService, SubscriberService } from 'nestjs-zeromq';
@Module({
imports: [
ZeroMQModule.forRoot({
sockets: [
{
type: 'publisher',
token: 'PUBLISHER_1',
options: { endpoint: 'tcp://127.0.0.1:5555' },
},
{
type: 'subscriber',
token: 'SUBSCRIBER_1',
options: {
endpoint: 'tcp://127.0.0.1:5555',
topics: ['news'],
},
},
],
}),
],
})
export class AppModule implements OnModuleInit {
@InjectPublisher('PUBLISHER_1')
private publisher: PublisherService;
@InjectSubscriber('SUBSCRIBER_1')
private subscriber: SubscriberService;
async onModuleInit() {
await this.publisher.publish('news', 'Hello ZeroMQ!');
this.subscriber.onMessage('news', (topic, message) => {
console.log('Received:', message.toString());
});
}
}Using Processor Pattern
import { Module } from '@nestjs/common';
import { ZeroMQModule, Subscriber, Process } from 'nestjs-zeromq';
import { Injectable } from '@nestjs/common';
@Subscriber('tcp://127.0.0.1:5555', { topics: ['news'] })
@Injectable()
export class NewsSubscriber {
@Process('news')
async handleNews(topic: string, message: Buffer) {
console.log(`[${topic}]`, message.toString());
}
}
@Module({
imports: [
ZeroMQModule.forRoot(),
ZeroMQModule.forFeature([NewsSubscriber]),
],
})
export class AppModule {}Documentation
For complete documentation, examples, and API reference, please see DOCS.md.
License
MIT
