0
0
NestJSframework~10 mins

TCP transport in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - TCP transport
Start NestJS App
Create TCP Server
Listen for TCP Connections
Receive TCP Message
Process Message in Handler
Send Response Back
Wait for Next Message or Connection
NestJS app starts and creates a TCP server that listens for messages, processes them, and sends responses.
Execution Sample
NestJS
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class AppController {
  @MessagePattern({ cmd: 'sum' })
  sum(data: number[]): number {
    return data.reduce((a, b) => a + b, 0);
  }
}
A NestJS controller listens for TCP messages with command 'sum' and returns the sum of numbers.
Execution Table
StepActionInput MessageHandler CalledHandler OutputResponse Sent
1TCP Server starts listeningN/AN/AN/AN/A
2Client sends message{ cmd: 'sum', data: [1,2,3] }sum66
3Client sends message{ cmd: 'sum', data: [10,20] }sum3030
4Client sends message{ cmd: 'sum', data: [] }sum00
5No more messagesN/AN/AN/AN/A
💡 No more messages received, TCP server continues listening.
Variable Tracker
VariableStartAfter 1After 2After 3Final
dataN/A[1,2,3][10,20][][]
handlerOutputN/A63000
Key Moments - 2 Insights
Why does the handler method 'sum' get called only when the message has cmd 'sum'?
Because the @MessagePattern decorator filters messages by the 'cmd' property, so only messages with cmd 'sum' trigger the sum method (see execution_table rows 2-4).
What happens if the client sends a message with a different command?
The TCP server ignores it or does not call the sum handler, so no response is sent for unknown commands (not shown in execution_table but implied).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the handler output at step 3?
A6
B0
C30
DN/A
💡 Hint
Check the 'Handler Output' column at step 3 in the execution_table.
At which step does the handler receive an empty array as input?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Input Message' column in execution_table for an empty data array.
If the client sends a message with cmd 'multiply', what happens?
ANo handler is called
BAn error is thrown
Csum handler is called with data
DResponse is always 0
💡 Hint
Recall that @MessagePattern filters by cmd 'sum' only, so other commands do not trigger handlers.
Concept Snapshot
NestJS TCP Transport Quick Guide:
- Use @MessagePattern({ cmd: 'name' }) to listen for TCP messages.
- TCP server listens continuously for messages.
- Handler methods process messages matching their cmd.
- Responses are sent back over TCP automatically.
- Unknown commands do not trigger handlers.
Full Transcript
This visual execution trace shows how NestJS uses TCP transport. The app starts and creates a TCP server that listens for messages. When a client sends a message with a command 'sum' and data array, the sum handler method is called. It calculates the sum of the numbers and sends the result back. This repeats for each message. If the message command does not match, no handler is called. Variables like data and handlerOutput change with each message. This helps beginners see how TCP messages flow through NestJS microservices.