📖 Scenario: You are building a simple NestJS microservice that communicates over TCP. This microservice will receive a message and respond with a greeting.
🎯 Goal: Create a NestJS microservice using TCP transport that listens for a message with the pattern { cmd: 'greet' } and responds with a greeting message.
📋 What You'll Learn
Create a NestJS microservice using TCP transport
Define a message pattern { cmd: 'greet' }
Implement a handler that returns a greeting string
Start the microservice to listen for TCP messages
💡 Why This Matters
🌍 Real World
TCP transport is used in microservices to enable fast and efficient communication between services in distributed systems.
💼 Career
Understanding TCP transport in NestJS is valuable for backend developers working with microservices architecture and building scalable server applications.
Progress0 / 4 steps
1
Create the microservice bootstrap function
Create a function called bootstrap that creates a NestJS microservice using TcpTransport from @nestjs/microservices. Use await to create the microservice instance and assign it to a variable called app.
NestJS
Hint
Use NestFactory.createMicroservice with AppModule and set transport to TcpTransport.
2
Add microservice listen configuration
Inside the bootstrap function, add a line to start the microservice listening by calling await app.listen().
NestJS
Hint
Call await app.listen() to start the microservice.
3
Create a message handler for the 'greet' command
In AppController, create a method called handleGreet decorated with @MessagePattern({ cmd: 'greet' }). This method should return the string 'Hello from TCP microservice!'.
NestJS
Hint
Use @MessagePattern decorator with the pattern { cmd: 'greet' } on the method.
4
Add AppController to AppModule providers
In AppModule, add AppController to the controllers array to register it.
NestJS
Hint
Include AppController in the controllers array of @Module.
Practice
(1/5)
1. What is the main purpose of using Transport.TCP in a NestJS microservice?
easy
A. To connect to a database using TCP.
B. To serve static files over HTTP.
C. To enable communication between microservices using TCP sockets.
D. To handle WebSocket connections.
Solution
Step 1: Understand the role of Transport.TCP
Transport.TCP is used in NestJS to set up microservices communication over TCP sockets.
Step 2: Compare with other options
Serving static files, database connections, and WebSocket handling are different concerns not related to Transport.TCP.
Final Answer:
To enable communication between microservices using TCP sockets. -> Option C
Quick Check:
Transport.TCP = Microservice TCP communication [OK]
Hint: Remember TCP transport is for microservice socket communication [OK]
Common Mistakes:
Confusing TCP transport with HTTP or WebSocket protocols
Thinking it handles database connections
Assuming it serves static files
2. Which of the following is the correct way to create a TCP microservice in NestJS?
The correct syntax uses transport: Transport.TCP and an options object with port.
Step 2: Identify incorrect options
The other options use wrong properties like protocol instead of transport, pass port directly without an options object, use string 'tcp' instead of the Transport.TCP enum, or use the wrong transport type like Transport.HTTP.
The port value is given as a string '3000', but it must be a number.
Step 2: Validate other parts
Host is optional, Transport.TCP is valid, and app.listen() can be awaited without callback.
Final Answer:
Port should be a number, not a string. -> Option A
Quick Check:
Port must be numeric type [OK]
Hint: Port option must be a number, not quoted string [OK]
Common Mistakes:
Passing port as string instead of number
Thinking host is mandatory
Expecting app.listen to require callback
5. You want to create a NestJS TCP microservice that listens on all network interfaces on port 5000 and logs a message when a client connects. Which code snippet correctly implements this?
To listen on all interfaces, host must be '0.0.0.0' and port 5000 is correct.
Step 2: Attach client connection event properly
TCP microservice emits 'connection' event; attaching listener after await app.listen() is correct.
Step 3: Evaluate distractors
Distractors either use the wrong host like 'localhost', omit the host, fail to await app.listen(), or attach the logger to the listen() callback instead of the 'connection' event.