NestJS Communication & Stakeholder Relations 2 — Questions and Answers
Question 1: Which NestJS transport layer is best suited for real-time bidirectional communication with browser clients?
- Redis
- WebSockets (Correct answer)
- gRPC
- RabbitMQ
Correct answer: WebSockets
NestJS WebSockets adapter enables full-duplex, real-time communication between server and browser clients.
Question 2: In a NestJS microservices architecture, what is the role of the @MessagePattern() decorator?
- It defines a REST endpoint
- It maps a message pattern to a handler method (Correct answer)
- It publishes events to a message broker
- It validates incoming DTO shapes
Correct answer: It maps a message pattern to a handler method
@MessagePattern() maps an incoming message pattern string to the controller method that should handle it.
Question 3: A stakeholder asks for API documentation that can be tested interactively. Which NestJS tool satisfies this requirement?
- Compodoc
- Swagger/OpenAPI via @nestjs/swagger (Correct answer)
- Jest
- class-validator
Correct answer: Swagger/OpenAPI via @nestjs/swagger
@nestjs/swagger generates an interactive Swagger UI that lets stakeholders explore and test API endpoints.
Question 4: When using NestJS Server-Sent Events (SSE), what return type must a controller method use?
- Promise<string>
- Observable<MessageEvent> (Correct answer)
- EventEmitter
- Stream<Buffer>
Correct answer: Observable<MessageEvent>
SSE endpoints must return an Observable<MessageEvent> so NestJS can stream events to the client.
Question 5: Which decorator in @nestjs/swagger marks a DTO property as required in the generated API schema?
- @IsNotEmpty()
- @ApiProperty({ required: true }) (Correct answer)
- @Required()
- @Column({ nullable: false })
Correct answer: @ApiProperty({ required: true })
@ApiProperty({ required: true }) tells Swagger to flag the property as required in the OpenAPI schema.
Question 6: A team wants to broadcast notifications to all connected WebSocket clients in NestJS. Which method is typically used?
- this.server.emit() (Correct answer)
- this.eventEmitter.emit()
- this.client.send()
- this.gateway.broadcast()
Correct answer: this.server.emit()
In a NestJS WebSocket gateway, this.server.emit() broadcasts a message to every connected socket client.
Question 7: When communicating with external stakeholders, which HTTP status code should a NestJS controller return to indicate a resource was successfully created?
- 200
- 201 (Correct answer)
- 204
- 202
Correct answer: 201
HTTP 201 Created is the correct response for a successful POST that results in a new resource being created.
Which NestJS transport layer is best suited for real-time bidirectional communication with browser clients?