0
0
NestJSframework~10 mins

Message patterns (request-response) in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Message patterns (request-response)
Client sends request message
Server receives message
Server processes message
Server sends response message
Client receives response
End
This flow shows how a client sends a request message, the server processes it, and then sends back a response message.
Execution Sample
NestJS
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class MathController {
  @MessagePattern({ cmd: 'sum' })
  sum(data: number[]): number {
    return data.reduce((a, b) => a + b, 0);
  }
}
This code listens for a 'sum' message pattern and returns the sum of numbers sent in the request.
Execution Table
StepActionInput MessageProcessingOutput Response
1Client sends message{ cmd: 'sum', data: [1, 2, 3] }N/AN/A
2Server receives message{ cmd: 'sum', data: [1, 2, 3] }Matches 'sum' patternN/A
3Server processes message[1, 2, 3]Calculates sum: 1+2+3=6N/A
4Server sends responseN/AN/A6
5Client receives responseN/AN/A6
6EndN/AN/AN/A
💡 Process ends after client receives the response 6.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
dataundefined[1, 2, 3][1, 2, 3][1, 2, 3]
sum resultundefinedundefined66
Key Moments - 2 Insights
Why does the server only respond when the message pattern matches?
Because the @MessagePattern decorator listens for specific patterns like { cmd: 'sum' }. If the incoming message does not match, the method is not called. See execution_table step 2.
What happens if the client sends data in a different format?
The server method expects an array of numbers. If the data is not an array, the sum calculation may fail or return unexpected results. This is shown in execution_table step 3 where data is processed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output response at step 4?
Aundefined
B6
C[1, 2, 3]
D{ cmd: 'sum' }
💡 Hint
Check the 'Output Response' column at step 4 in the execution_table.
At which step does the server match the message pattern?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look at the 'Processing' column in execution_table to find when pattern matching happens.
If the client sends data [4, 5], what would be the sum result after step 3?
Aundefined
B45
C9
DError
💡 Hint
Refer to variable_tracker for how sum result is calculated from data array.
Concept Snapshot
NestJS Message Patterns (request-response):
- Use @MessagePattern({ cmd: 'name' }) to listen for messages.
- Client sends a message with matching pattern and data.
- Server processes data and returns a response.
- Response is sent back to client automatically.
- Useful for microservices communication with clear request-response flow.
Full Transcript
In NestJS, message patterns allow microservices to communicate using a request-response style. The client sends a message with a specific pattern, such as { cmd: 'sum' }, along with data. The server listens for this pattern using the @MessagePattern decorator. When the server receives a matching message, it processes the data—in this example, summing an array of numbers—and sends the result back to the client. The client then receives the response and can use it as needed. This flow ensures clear communication between services with defined message patterns and responses.