NestJS NestJS 5 — Questions and Answers
Question 1: Which NestJS package provides WebSocket gateway support?
- @nestjs/http
- @nestjs/websockets (Correct answer)
- @nestjs/socket
- @nestjs/gateway
Correct answer: @nestjs/websockets
The @nestjs/websockets package (combined with @nestjs/platform-socket.io or @nestjs/platform-ws) provides WebSocket support in NestJS via the @WebSocketGateway() decorator.
Question 2: What decorator is used to define a NestJS WebSocket gateway?
- @Gateway()
- @Socket()
- @WebSocketGateway() (Correct answer)
- @WsGateway()
Correct answer: @WebSocketGateway()
@WebSocketGateway() decorates a class to define it as a WebSocket gateway, with optional port and namespace configuration.
Question 3: In NestJS microservices, which transport layer uses Redis for message passing?
- TCP
- MQTT
- Redis (Correct answer)
- gRPC
Correct answer: Redis
NestJS includes a built-in Redis transport layer that uses Redis pub/sub for microservice communication between services.
Question 4: Which decorator marks a NestJS microservice message handler for a specific pattern?
- @Subscribe()
- @MessagePattern() (Correct answer)
- @EventHandler()
- @Listen()
Correct answer: @MessagePattern()
@MessagePattern() decorates a method in a microservice controller to handle messages matching a specific pattern sent by a client.
Question 5: What is the purpose of ClientProxy in NestJS microservices?
- To proxy HTTP requests to external APIs
- To send messages and emit events to a microservice from a client application (Correct answer)
- To create proxy controllers automatically
- To wrap database clients for microservices
Correct answer: To send messages and emit events to a microservice from a client application
ClientProxy is injected into client applications using @Inject() with a client token and exposes send() and emit() methods to communicate with microservices.
Question 6: In NestJS, what is the difference between send() and emit() on a ClientProxy?
- send() is for HTTP; emit() is for WebSocket
- send() expects a response (request-response); emit() is fire-and-forget (event-based) (Correct answer)
- send() is synchronous; emit() is asynchronous
- There is no functional difference
Correct answer: send() expects a response (request-response); emit() is fire-and-forget (event-based)
send() implements the request-response pattern and returns an Observable with the response, while emit() publishes an event without waiting for a response.
Question 7: Which NestJS feature allows you to schedule recurring tasks using cron expressions?
- @nestjs/schedule with @Cron() (Correct answer)
- @nestjs/tasks with @Recurring()
- @nestjs/cron with @Schedule()
- @nestjs/jobs with @CronJob()
Correct answer: @nestjs/schedule with @Cron()
The @nestjs/schedule package provides the @Cron() decorator (along with @Interval() and @Timeout()) to schedule recurring tasks using standard cron expressions.
Which NestJS package provides WebSocket gateway support?