nestjstools/messaging
Simplifies asynchronous and synchronous message handling with support for buses, handlers, channels, and consumers. Build scalable, decoupled applications with ease and reliability.
A powerful message bus and service bus library for NestJS designed for building scalable, event-driven and distributed systems.
NestJSTools Messaging provides a broker-independent messaging abstraction that allows applications to communicate through commands, events and queries using a flexible message bus architecture.
The framework integrates seamlessly with the NestJS dependency injection system, decorators and modules, making it easy to build CQRS architectures, microservices and event-driven applications.
It supports multiple messaging transports including RabbitMQ, Redis, NATS, Amazon SQS, Google Pub/Sub and Azure Service Bus, allowing developers to switch brokers without rewriting application logic.
This repository contains the core messaging framework and official transport extensions for integrating different messaging infrastructures.
NestJSTools Messaging helps you build scalable, event-driven and distributed systems in NestJS without coupling your application to a specific messaging broker.
Key capabilities include:
No download data available
No tracked packages depend on this.
Supported transports include:
| Package | Description |
|---|---|
@nestjstools/messaging | Core messaging framework |
| Package | Description |
|---|---|
@nestjstools/messaging-redis-extension | Redis channel adapter |
@nestjstools/messaging-rabbitmq-extension | RabbitMQ channel adapter |
@nestjstools/messaging-amazon-sqs-extension | Amazon SQS adapter |
@nestjstools/messaging-google-pubsub-extension | Google Pub/Sub adapter |
@nestjstools/messaging-nats-extension | NATS adapter |
@nestjstools/messaging-azure-service-bus-extension | Azure Service Bus adapter |
Install the core package:
npm install @nestjstools/messaging
or
yarn add @nestjstools/messaging
Then install a transport extension if needed:
npm install @nestjstools/messaging-rabbitmq-extension
Below is a minimal example showing how to:
export class SendMessage {
constructor(
public readonly content: string,
) {
}
}
import { Injectable } from '@nestjs/common';
import {
MessageHandler,
IMessageHandler,
} from '@nestjstools/messaging';
import { SendMessage } from './send-message';
@Injectable()
@MessageHandler('chat.message')
export class SendMessageHandler implements IMessageHandler<SendMessage> {
async handle(message: SendMessage): Promise<void> {
console.log('Received message:', message.content);
}
}
import { Module } from '@nestjs/common';
import { MessagingModule, InMemoryChannelConfig } from '@nestjstools/messaging';
import { SendMessageHandler } from './send-message.handler';
@Module({
imports: [
MessagingModule.forRoot({
buses: [
{
name: 'message.bus',
channels: ['memory'],
},
],
channels: [
new InMemoryChannelConfig({
name: 'memory',
}),
],
debug: true,
}),
],
providers: [SendMessageHandler],
})
export class AppModule {
}
Messages can be dispatched from anywhere in your application.
Example using a controller:
import { Controller, Get } from '@nestjs/common';
import { IMessageBus, MessageBus, RoutingMessage } from '@nestjstools/messaging';
import { SendMessage } from './send-message';
@Controller()
export class AppController {
constructor(
@MessageBus('message.bus')
private readonly messageBus: IMessageBus,
) {
}
@Get()
async send(): Promise<string> {
await this.messageBus.dispatch(
new RoutingMessage(
new SendMessage('Hello from NestJSTools!'),
'chat.message',
),
);
return 'Message dispatched!';
}
}
flowchart LR
M[Message]
MB[Message Bus]
C[(Channel)]
MW[Middleware]
H[Message Handler]
M -->|dispatch| MB
MB --> C
C --> MW
MW --> H
flowchart LR
M[Message]
MB[MessageBus]
C[InMemoryChannel]
MW[Middleware]
H[MessageHandler]
EL[ExceptionListener]
M -->|dispatch| MB
MB --> C
C --> MW
MW --> H
H -. throws error .-> C
MW -. throws error .-> C
C -. notify .-> EL
Contributions are welcome!
Please open an issue or pull request if you want to:
Before opening a pull request, please read the contribution guide: CONTRIBUTING.md
It explains:
If you like this project please star the repository ⭐ It helps the project grow and reach more developers.
nestjs messaging library
nestjs message bus
nestjs service bus
nestjs event bus
nestjs distributed systems
nestjs microservices messaging
broker independent messaging for nestjs
nestjs rabbitmq abstraction
nestjs redis messaging
nestjs nats messaging