Skip to content

TcpClientOptions

reference
2 min readUpdated

Kind: Interface

Source: packages/microservices/interfaces/client-metadata.interface.ts

Part of: Microservices

TcpClientOptions configures a microservice client that communicates over the TCP transport. It defines the target host and port, optional serialization behavior, TLS settings, socket implementation, and the maximum inbound buffer size.

Properties

PropertyType
transportTransport.TCP
options{ host?: string; port?: number; serializer?: Serializer; deserializer?: Deserializer; tlsOptions?: ConnectionOptions; socketClass?: Type<TcpSocket>; maxBufferSize?: number; }

Diagram

mermaid
graph LR
  Client[TCP Microservice Client] --> Config[TcpClientOptions]
  Config --> Transport[transport: Transport.TCP]
  Config --> Connection[options]
  Connection --> Host[host]
  Connection --> Port[port]
  Connection --> Serialization[serializer / deserializer]
  Connection --> TLS[tlsOptions]
  Connection --> Socket[socketClass]
  Connection --> Buffer[maxBufferSize]
  Client --> Server[TCP Server]

Usage

ts
import { ClientProxyFactory, Transport } from '@nestjs/microservices';
import type { TcpClientOptions } from '@nestjs/microservices';

const tcpOptions: TcpClientOptions = {
  transport: Transport.TCP,
  options: {
    host: '127.0.0.1',
    port: 3001,
    maxBufferSize: 1024 * 1024,
  },
};

const client = ClientProxyFactory.create(tcpOptions);

client.send({ cmd: 'get_user' }, { id: '123' }).subscribe(response => {
  console.log(response);
});

AI Coding Instructions

  • Always set transport to Transport.TCP; this interface is only valid for TCP client configurations.
  • Provide host and port values that match the TCP microservice server configuration.
  • Use matching serializer and deserializer implementations on both client and server when customizing message encoding.
  • Configure tlsOptions only when connecting to a TLS-enabled TCP server, using compatible Node.js ConnectionOptions.
  • Set maxBufferSize appropriately for expected payload sizes to prevent oversized TCP message buffering.

How it works

TcpClientOptions is the public TypeScript configuration interface for a TCP microservice client. Its transport field is required and must be Transport.TCP; it is one member of the ClientOptions union. client-metadata.interface.ts:17-24 client-metadata.interface.ts:34-38

Was this page helpful?

Download as PDF
TcpClientOptions — NestJS head-to-head