Nestjstools Messaging Docs
  • Introduction
    • What is this library?
    • Supported message brokers
  • Getting Started
    • Installation
    • Initialize Module
    • Message Handler
    • Disaptch a message
  • Components
    • Message Handlers
    • Normalizers
    • Exception Listeners
    • Middlewares
    • Message Bus
    • Channel
  • Broker integration
    • RabbitMQ
    • Redis
    • Google PubSub
    • Amazon SQS
    • Nats
  • Best practice
    • CQRS based on RabbitMQ
    • Create wrapper class for Message Bus
    • Consumer as the background process
Powered by GitBook
On this page
  • 📡 Channels
  • 🔍 What Is a Channel?
  • 🛠 Defining a Channel
  • 🧩 Channel Configuration Options
  • 🔄 Channels and Buses
  • 🧩 Middleware and Normalizers on Channels
  • Benefits of Using Channels
Export as PDF
  1. Components

Channel

📡 Channels

In the @nestjstools/messaging library, Channels represent the underlying transport mechanisms that deliver messages between microservices. Each channel is responsible for handling message transmission, reception, serialization, and routing to the appropriate handlers.

Channels abstract the communication details, allowing your application to work seamlessly whether using in-memory queues, RabbitMQ, or any other supported transport.


🔍 What Is a Channel?

A Channel is a pluggable component that:

  • Sends and receives messages over a specific transport protocol

  • Applies serialization and deserialization (normalizers)

  • Manages queues, exchanges, topics, or other infrastructure details

  • Supports middleware to process messages in the pipeline


🛠 Defining a Channel

Channels are configured within the MessagingModule via channel config classes. Each channel requires:

  • A unique name

  • Channel-specific options (connection info, queues, bindings)

  • Optional middlewares and normalizers

Example: InMemory Channel

import { InMemoryChannelConfig } from '@nestjstools/messaging';

new InMemoryChannelConfig({
  name: 'my-channel',
  middlewares: [LoggingMiddleware],
});

Example: AMQP Channel

import { AmqpChannelConfig, ExchangeType } from '@nestjstools/messaging';

new AmqpChannelConfig({
  name: 'amqp-command',
  connectionUri: 'amqp://guest:guest@localhost:5672/',
  exchangeName: 'my_app_command.exchange',
  bindingKeys: ['my_app.command.#'],
  exchangeType: ExchangeType.TOPIC,
  queue: 'my_app.command',
  autoCreate: true,
  enableConsumer: true,
});

🧩 Channel Configuration Options

Option
Description

name

Unique identifier of the channel

middlewares

List of middleware classes to apply on messages

normalizer

Serializer/deserializer to encode/decode messages

Transport-specific options

Connection strings, queues, exchange names, binding keys, etc.

enableConsumer

Enables or disables the RabbitMQ consumer for this channel


🔄 Channels and Buses

A Bus is connected to one or more channels. When a message is dispatched to a bus, it is forwarded to all its associated channels.

This design allows you to:

  • Send messages to multiple transports simultaneously

  • Mix different messaging protocols within the same application

  • Configure channels independently for flexibility and scaling


🧩 Middleware and Normalizers on Channels

Each channel can have its own middleware stack and normalizers, enabling per-transport processing and custom encoding/decoding.


Benefits of Using Channels

Feature
Benefit

🔌 Transport Abstraction

Switch between RabbitMQ, in-memory, or other protocols easily

⚙️ Flexible Configuration

Fine-tune transport settings independently per channel

🧩 Extensible Middleware

Add custom logic like logging, auth, or metrics per channel

🔄 Multi-Channel Support

Dispatch messages across multiple transports simultaneously

PreviousMessage BusNextBroker integration

Last updated 9 days ago