const client = ClientProxyFactory.create({ transport: Transport.TCP, options: { host: '127.0.0.1', port: 3001 } });
async function sendMessage() {
return client.send('test_pattern', { data: 'hello' }).toPromise();
}
sendMessage().catch(err => console.error(err));By default, NestJS TCP clients will attempt to reconnect and retry sending messages if the server is not available. This means the message is queued and retried until the server responds or the client times out.
The correct way to configure a TCP microservice server in NestJS is to specify the transport as Transport.TCP and provide the port inside the options object.
"ECONNREFUSED" indicates that the client tried to connect but no server was listening on the target port, so the connection was refused.
const client = ClientProxyFactory.create({
transport: Transport.TCP,
options: { host: '127.0.0.1', port: 3001, timeout: 2000 }
});
async function send() {
try {
const response = await client.send('delayed_response', {}).toPromise();
console.log('Response:', response);
} catch (err) {
console.log('Error:', err.message);
}
}
send();The timeout option causes the client to throw a timeout error if the server does not respond within the specified time.
The server uses the pattern string sent by the client to find the matching @MessagePattern handler method.
