Create custom channel
@nestjstools/messaging allows you to create custom transports by implementing your own Channel, MessageBus, and Consumer.
This makes it possible to integrate the messaging system with external technologies such as:
RabbitMQ
Redis
NATS
Google Pub/Sub
AWS SQS
or any other messaging system
A custom channel acts as the transport layer responsible for delivering and consuming messages.
1. Create a ChannelConfig
ChannelConfig stores configuration required to establish a connection to the messaging system.
import { ChannelConfig } from '@nestjstools/messaging';
export class YourChannelConfig extends ChannelConfig {
public readonly connectionUri: string;
public readonly queue: string;
constructor({
name,
connectionUri,
queue,
avoidErrorsForNotExistedHandlers,
middlewares,
enableConsumer,
normalizer,
}: {
name: string;
connectionUri: string;
queue: string;
avoidErrorsForNotExistedHandlers?: boolean;
middlewares?: object[];
enableConsumer?: boolean;
normalizer?: object;
}) {
super(
name,
avoidErrorsForNotExistedHandlers,
middlewares,
enableConsumer,
normalizer,
);
this.connectionUri = connectionUri;
this.queue = queue;
}
}Typical configuration may include:
connection settings
channel name
middleware configuration
transport-specific options
2. Create a Channel
The Channel acts as the data source layer and manages the connection to the external service.
This class can manage:
connections
transport resources
3. Create a ChannelFactory
The ChannelFactory creates channel instances and integrates them with NestJS dependency injection.
4. Create a MessageBus
The MessageBus is responsible for dispatching messages to the transport layer.
This is where you integrate with your messaging system (RabbitMQ, Redis, etc.).
5. Create a MessageBusFactory
The MessageBusFactory creates instances of your message bus.
6. Create a Consumer
A consumer reads messages from the transport and dispatches them to handlers inside the application.
The consumer should:
read messages from the messaging system
dispatch them to application handlers
handle processing errors
7. Custom MessageOptions (Optional)
You can define custom message options for your transport and build custom logic like adding headers etc.
Registering Providers
Classes decorated with @Injectable() must be registered as providers in your NestJS module.
Last updated
Was this helpful?