0
0
NestJSframework~20 mins

TCP transport in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TCP Transport Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding TCP Transport Client Behavior in NestJS
Consider a NestJS microservice client configured with TCP transport connecting to a server. What will happen if the server is not running when the client tries to send a message?
NestJS
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));
AThe client will throw an error immediately because the server is unreachable.
BThe client will connect to a default fallback server automatically.
CThe client will silently drop the message without any error or retry.
DThe client will queue the message and retry sending until the server becomes available.
Attempts:
2 left
💡 Hint
Think about how NestJS TCP clients handle connection retries by default.
📝 Syntax
intermediate
2:00remaining
Correct TCP Transport Server Setup in NestJS
Which of the following code snippets correctly creates a TCP microservice server in NestJS listening on port 4000?
A
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, options: { port: 4000 } });
await app.listen();
B
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, port: 4000 });
await app.listen();
C
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, options: { host: 'localhost' } });
await app.listen();
D
const app = await NestFactory.createMicroservice(AppModule, { transport: 'tcp', options: { port: 4000 } });
await app.listen();
Attempts:
2 left
💡 Hint
Check how the port is specified inside the options object.
🔧 Debug
advanced
2:00remaining
Diagnosing TCP Transport Connection Refused Error
A NestJS TCP client throws a "connect ECONNREFUSED" error when trying to send a message. Which of the following is the most likely cause?
AThe client and server have mismatched message payload formats.
BThe client is using the wrong message pattern name.
CThe server is not running or not listening on the specified port.
DThe client forgot to import the Transport module.
Attempts:
2 left
💡 Hint
ECONNREFUSED means the connection was actively refused by the target machine.
state_output
advanced
2:00remaining
Output of TCP Client Send with Timeout
Given a NestJS TCP client configured with a 2-second timeout, what will be the output if the server takes 5 seconds to respond?
NestJS
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();
ALogs: 'Error: Timeout has occurred' after 2 seconds.
BLogs: 'Error: Connection refused' immediately.
CLogs: 'Response: <server response>' after 5 seconds.
DNo output; the client waits indefinitely.
Attempts:
2 left
💡 Hint
Consider the effect of the timeout option on client requests.
🧠 Conceptual
expert
3:00remaining
TCP Transport Message Pattern Matching in NestJS
In a NestJS TCP microservice, how does the server determine which handler to invoke for an incoming message?
AIt uses the message payload type to select the handler automatically.
BIt matches the message's pattern property to the @MessagePattern decorator's pattern on handlers.
CIt invokes all handlers and returns the first non-null response.
DIt relies on the client to specify the handler function name explicitly.
Attempts:
2 left
💡 Hint
Think about how NestJS uses decorators to route messages.