CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-nestjs--microservices

Nest microservices framework providing scalable distributed systems with multiple transport layers and communication patterns.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

transports.mddocs/

Transport Implementations

Support for multiple messaging protocols including TCP, Redis, NATS, MQTT, gRPC, RabbitMQ, and Kafka with protocol-specific features and configurations for building distributed microservice architectures.

Capabilities

Transport Enum

Enumeration of available transport types for microservice communication.

/**
 * Available transport types for microservice communication
 */
enum Transport {
  TCP = 0,
  REDIS = 1,
  NATS = 2,
  MQTT = 3,
  GRPC = 4,
  RMQ = 5,
  KAFKA = 6
}

Microservice Configuration

Configuration interface for setting up microservice transport options.

/**
 * Microservice configuration options
 */
interface MicroserviceOptions {
  /** Transport type or custom transport strategy */
  transport?: Transport | CustomTransportStrategy;
  /** Transport-specific configuration options */
  options?: any;
}

/**
 * Custom transport strategy interface
 */
interface CustomTransportStrategy {
  server: any;
  client: any;
}

Usage Examples:

import { NestFactory } from '@nestjs/core';
import { Transport, MicroserviceOptions } from '@nestjs/microservices';

// TCP microservice
const tcpApp = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
  transport: Transport.TCP,
  options: {
    host: '127.0.0.1',
    port: 3001,
  },
});

// Redis microservice
const redisApp = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
  transport: Transport.REDIS,
  options: {
    host: 'localhost',
    port: 6379,
    retryAttempts: 5,
    retryDelay: 3000,
  },
});

TCP Transport

Simple, reliable TCP socket-based communication for high-performance local networks.

/**
 * TCP transport server implementation
 */
class ServerTCP extends Server {
  constructor(options: TcpOptions);
}

/**
 * TCP transport client implementation
 */
class ClientTCP extends ClientProxy {
  constructor(options: TcpOptions);
}

interface TcpOptions {
  /** Server host address */
  host?: string;
  /** Server port number */
  port?: number;
  /** Custom socket class */
  socketClass?: any;
  /** TLS encryption options */
  tlsOptions?: any;
}

Usage Examples:

// TCP Server
const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.TCP,
  options: {
    host: '0.0.0.0',
    port: 3001,
    tlsOptions: {
      key: fs.readFileSync('server-key.pem'),
      cert: fs.readFileSync('server-cert.pem'),
    },
  },
});

// TCP Client
const client = ClientProxyFactory.create({
  transport: Transport.TCP,
  options: {
    host: '127.0.0.1',
    port: 3001,
  },
});

Redis Transport

Redis pub/sub based transport for scalable message broadcasting and caching integration.

/**
 * Redis transport server implementation
 */
class ServerRedis extends Server {
  constructor(options: RedisOptions);
}

/**
 * Redis transport client implementation
 */
class ClientRedis extends ClientProxy {
  constructor(options: RedisOptions);
  getRequestPattern(pattern: string): string;
  getReplyPattern(pattern: string): string;
}

interface RedisOptions {
  /** Redis server host */
  host?: string;
  /** Redis server port */
  port?: number;
  /** Redis password */
  password?: string;
  /** Redis database number */
  db?: number;
  /** Connection retry attempts */
  retryAttempts?: number;
  /** Retry delay in milliseconds */
  retryDelay?: number;
}

NATS Transport

Lightweight, high-performance messaging system with subject-based routing.

/**
 * NATS transport server implementation
 */
class ServerNats extends Server {
  constructor(options: NatsOptions);
}

/**
 * NATS transport client implementation
 */
class ClientNats extends ClientProxy {
  constructor(options: NatsOptions);
  createClient(): Promise<any>;
}

interface NatsOptions {
  /** NATS server URLs */
  servers?: string[];
  /** Client name */
  name?: string;
  /** Authentication user */
  user?: string;
  /** Authentication password */
  pass?: string;
  /** Connection timeout */
  timeout?: number;
}

MQTT Transport

Lightweight publish-subscribe messaging protocol ideal for IoT and mobile applications.

/**
 * MQTT transport server implementation
 */
class ServerMqtt extends Server {
  constructor(options: MqttOptions);
}

/**
 * MQTT transport client implementation
 */
class ClientMqtt extends ClientProxy {
  constructor(options: MqttOptions);
  getRequestPattern(pattern: string): string;
  getResponsePattern(pattern: string): string;
}

interface MqttOptions {
  /** MQTT broker URL */
  url?: string;
  /** Client identifier */
  clientId?: string;
  /** Authentication username */
  username?: string;
  /** Authentication password */
  password?: string;
  /** Quality of service level */
  qos?: 0 | 1 | 2;
  /** Retain messages flag */
  retain?: boolean;
}

gRPC Transport

High-performance RPC framework with protocol buffer serialization and streaming support.

/**
 * gRPC transport server implementation
 */
class ServerGrpc extends Server {
  constructor(options: GrpcOptions);
}

/**
 * gRPC transport client implementation
 */
class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
  constructor(options: GrpcOptions);
  getService<T>(name: string): T;
  getClientByServiceName<T>(name: string): T;
}

interface GrpcOptions {
  /** Protocol buffer package name */
  package: string | string[];
  /** Path to .proto files */
  protoPath: string | string[];
  /** gRPC server URL */
  url?: string;
  /** Proto loader options */
  loader?: any;
  /** gRPC credentials */
  credentials?: any;
}

Usage Examples:

// gRPC Server
const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.GRPC,
  options: {
    package: 'hero',
    protoPath: path.join(__dirname, 'hero.proto'),
    url: '0.0.0.0:5000',
    loader: {
      keepCase: true,
      longs: String,
      enums: String,
      defaults: true,
      oneofs: true,
    },
  },
});

// gRPC Client
const client = ClientProxyFactory.create({
  transport: Transport.GRPC,
  options: {
    package: 'hero',
    protoPath: path.join(__dirname, 'hero.proto'),
    url: 'localhost:5000',
  },
}) as ClientGrpc;

const heroService = client.getService<HeroService>('HeroService');

RabbitMQ Transport

Advanced message queuing with routing, durability, and complex messaging patterns.

/**
 * RabbitMQ transport server implementation
 */
class ServerRMQ extends Server {
  constructor(options: RmqOptions);
}

/**
 * RabbitMQ transport client implementation
 */
class ClientRMQ extends ClientProxy {
  constructor(options: RmqOptions);
  createChannel(): Promise<void>;
}

interface RmqOptions {
  /** RabbitMQ connection URLs */
  urls?: string[];
  /** Default queue name */
  queue?: string;
  /** Queue declaration options */
  queueOptions?: any;
  /** Prefetch count for consumers */
  prefetchCount?: number;
  /** Channel setup callback */
  socketOptions?: any;
}

Kafka Transport

High-throughput, distributed streaming platform with partitioning and replication.

/**
 * Kafka transport server implementation
 */
class ServerKafka extends Server {
  constructor(options: KafkaOptions);
}

/**
 * Kafka transport client implementation
 */
class ClientKafka extends ClientProxy {
  constructor(options: KafkaOptions);
  subscribeToResponseOf(pattern: unknown): void;
  emitBatch<TResult, TInput>(
    pattern: any,
    data: { messages: TInput[] }
  ): Observable<TResult>;
  commitOffsets(topicPartitions: TopicPartitionOffsetAndMetadata[]): Promise<void>;
}

interface KafkaOptions {
  /** Kafka client configuration */
  client: {
    clientId: string;
    brokers: string[];
    ssl?: any;
    sasl?: any;
  };
  /** Consumer group configuration */
  consumer?: {
    groupId: string;
    allowAutoTopicCreation?: boolean;
    sessionTimeout?: number;
    heartbeatInterval?: number;
  };
  /** Producer configuration */
  producer?: {
    allowAutoTopicCreation?: boolean;
    transactionTimeout?: number;
    maxInFlightRequests?: number;
  };
}

Usage Examples:

// Kafka Server
const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.KAFKA,
  options: {
    client: {
      clientId: 'hero-server',
      brokers: ['localhost:9092'],
    },
    consumer: {
      groupId: 'hero-consumer',
    },
  },
});

// Kafka Client with batch operations
const kafkaClient = ClientProxyFactory.create({
  transport: Transport.KAFKA,
  options: {
    client: {
      clientId: 'hero-client',
      brokers: ['localhost:9092'],
    },
  },
});

// Send batch of messages
await kafkaClient.emitBatch('user.events', {
  messages: [
    { key: 'user1', value: { action: 'login', timestamp: Date.now() } },
    { key: 'user2', value: { action: 'logout', timestamp: Date.now() } },
  ],
}).toPromise();

Server Base Classes

Base server implementations for transport-specific servers.

/**
 * Base server class for all transport implementations
 */
abstract class Server {
  abstract listen(callback: () => void): void;
  abstract close(): void;
}

/**
 * Server factory for creating transport-specific servers
 */
class ServerFactory {
  static create(
    microserviceOptions: MicroserviceOptions
  ): Server & CustomTransportStrategy;
}

docs

client-proxies.md

context-objects.md

exceptions.md

grpc.md

index.md

message-patterns.md

modules.md

transports.md

tile.json