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
| Property | Type |
|---|---|
transport | Transport.TCP |
options | { host?: string; port?: number; serializer?: Serializer; deserializer?: Deserializer; tlsOptions?: ConnectionOptions; socketClass?: Type<TcpSocket>; maxBufferSize?: number; } |
Diagram
mermaidgraph 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
tsimport { 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
transporttoTransport.TCP; this interface is only valid for TCP client configurations. - Provide
hostandportvalues that match the TCP microservice server configuration. - Use matching
serializeranddeserializerimplementations on both client and server when customizing message encoding. - Configure
tlsOptionsonly when connecting to a TLS-enabled TCP server, using compatible Node.jsConnectionOptions. - Set
maxBufferSizeappropriately 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
optionsis optional. When present, it may containhost,port,serializer,deserializer,tlsOptions,socketClass, andmaxBufferSize. client-metadata.interface.ts:37-51ClientProxyFactory.create()defaults a missingoptionsobject to{}and, for TCP/default transport selection, constructsClientTCPwith those options. client-proxy-factory.ts:42-49 client-proxy-factory.ts:70-73hostandportselect the connection endpoint. If absent,ClientTCPuseslocalhostand3000, respectively. client-tcp.ts:30-33 constants.ts:3-4serializermust implementserialize(value, options?);deserializermust implementdeserialize(value, options?), which may return a value or promise. If either is absent or falsy, the client initializes an identity serializer or incoming-response deserializer, respectively. serializer.interface.ts:10-12 deserializer.interface.ts:10-15 client-proxy.ts:208-231tlsOptionshas Node TLSConnectionOptionstype. When it is set, the client creates its underlying socket withtls.connect, merging these options while overriding itsportandhost; otherwise it creates anet.Socketand explicitly connects it duringconnect(). client-metadata.interface.ts:42-45 client-tcp.ts:65-68 client-tcp.ts:99-121socketClassis aTcpSocketconstructor. It defaults toJsonSocket. client-metadata.interface.ts:44-45 client-tcp.ts:32-36maxBufferSizeis passed only whensocketClassis exactly the built-inJsonSocket; custom socket classes receive no such constructor option. client-tcp.ts:113-121 ForJsonSocket, an omitted value defaults to(512 * 1024 * 1024) / 4characters. json-socket.ts:7-11 json-socket.ts:21-24- With
JsonSocket, received data accumulates in a string buffer. If its length exceedsmaxBufferSize, the buffer is cleared andMaxPacketLengthExceededExceptionis thrown; the base socket catches this, emits an error, and ends the socket. json-socket.ts:30-43 tcp-socket.ts:54-60 - The interface itself declares no runtime validation for option values. The visible TCP constructor reads the fields and initializes serializer/deserializer without checks. client-tcp.ts:30-40
Was this page helpful?