What is the purpose of the @WebSocketGateway decorator in NestJS?
The <code>@WebSocketGateway</code> decorator marks a class as a WebSocket gateway, enabling it to handle real-time communication using WebSockets in a NestJS application.
Click to reveal answer
intermediate
How do you specify the WebSocket server options using the @WebSocketGateway decorator?
You pass an options object to the @WebSocketGateway decorator, for example: @WebSocketGateway({ namespace: '/chat', transports: ['websocket'] }) to configure namespace and transport methods.
Click to reveal answer
beginner
Which NestJS module must be imported to use the @WebSocketGateway decorator?
You must import <code>@WebSocketGateway</code> from <code>@nestjs/websockets</code> to use the <code>@WebSocketGateway</code> decorator in your class.
Click to reveal answer
intermediate
Can a class decorated with <code>@WebSocketGateway</code> handle multiple WebSocket events? How?
Yes, by adding methods decorated with @SubscribeMessage('eventName') inside the gateway class, you can handle multiple WebSocket events.
Click to reveal answer
advanced
What is the difference between @Gateway and @WebSocketGateway decorators in NestJS?
In NestJS, @WebSocketGateway is the correct decorator to create a WebSocket gateway. @Gateway is not a standard decorator; the official one is @WebSocketGateway. Sometimes @Gateway is used informally to refer to it.
Click to reveal answer
Which decorator do you use to create a WebSocket gateway in NestJS?
A@WebSocketGateway
B@Gateway
C@SocketServer
D@SubscribeMessage
✗ Incorrect
The correct decorator to create a WebSocket gateway is @WebSocketGateway.
How do you listen to a WebSocket event named 'message' inside a gateway class?
AUse @Listen('message') on a method
BUse @OnEvent('message') on a method
CUse @SubscribeMessage('message') on a method
DUse @WebSocketListener('message') on a method
✗ Incorrect
You use @SubscribeMessage('message') to listen to WebSocket events named 'message'.
Where do you import the @WebSocketGateway decorator from?
A@nestjs/common
B@nestjs/websockets
Csocket.io
D@nestjs/core
✗ Incorrect
The @WebSocketGateway decorator is imported from @nestjs/websockets.
Which option can you set in the @WebSocketGateway decorator?
Anamespace
Bdatabase
Ctemplate
Dmiddleware
✗ Incorrect
You can set the 'namespace' option to define the WebSocket namespace.
What does the @SocketServer decorator do inside a gateway class?
AConfigures transport options
BMarks a method as an event handler
CDefines the gateway namespace
DInjects the WebSocket server instance
✗ Incorrect
@SocketServer injects the WebSocket server instance to use inside the gateway.
Explain how to create a WebSocket gateway in NestJS and handle a custom event.
Think about the decorator for the class and the decorator for event methods.
You got /4 concepts.
Describe how to configure a WebSocket gateway namespace and access the server instance.
Focus on decorator options and property decorators inside the class.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of the @WebSocketGateway() decorator in NestJS?
easy
A. To turn a class into a WebSocket gateway for real-time communication
B. To create a REST API controller
C. To define a database entity
D. To handle HTTP middleware
Solution
Step 1: Understand the role of @WebSocketGateway()
This decorator marks a class as a WebSocket gateway, enabling real-time communication between server and clients.
Step 2: Compare with other options
Options B, C, and D relate to REST controllers, database entities, and middleware, which are unrelated to WebSocket gateways.
Final Answer:
To turn a class into a WebSocket gateway for real-time communication -> Option A
In NestJS, message handlers should return the response payload, not emit directly on the client.
Step 2: Analyze the code's client.emit usage
Calling client.emit is incorrect; the framework handles sending the return value automatically.
Final Answer:
The method handleMessage should return the payload instead of emitting -> Option B
Quick Check:
Return payload in handler, don't emit manually [OK]
Hint: Return data in handler; don't call client.emit manually [OK]
Common Mistakes:
Calling client.emit instead of returning data
Thinking @SubscribeMessage needs parentheses
Assuming handler must be async always
5.
You want to create a WebSocket gateway that listens on port 8081 and uses the namespace /notifications. Which of the following is the correct way to define this gateway?
hard
A. @WebSocketGateway({ port: 8081, namespace: '/notifications' })
export class NotificationGateway {}
B. @WebSocketGateway('/notifications', 8081)
export class NotificationGateway {}
C. @WebSocketGateway({ port: 8081 })
@WebSocketGateway({ namespace: '/notifications' })
export class NotificationGateway {}
D. @WebSocketGateway(8081, { namespace: '/notifications' })
export class NotificationGateway {}
Solution
Step 1: Recall the correct options object usage
The @WebSocketGateway() decorator accepts a single options object where you can specify both port and namespace.
Step 2: Evaluate each option
@WebSocketGateway({ port: 8081, namespace: '/notifications' })
export class NotificationGateway {} correctly passes an object with both port and namespace. @WebSocketGateway(8081, { namespace: '/notifications' })
export class NotificationGateway {} incorrectly uses positional arguments mixing number and object. @WebSocketGateway({ port: 8081 })
@WebSocketGateway({ namespace: '/notifications' })
export class NotificationGateway {} uses two decorators which is invalid. @WebSocketGateway('/notifications', 8081)
export class NotificationGateway {} uses positional arguments in wrong order and types.
Final Answer:
@WebSocketGateway({ port: 8081, namespace: '/notifications' })
export class NotificationGateway {} -> Option A
Quick Check:
Use single options object for port and namespace [OK]
Hint: Use one options object with port and namespace keys [OK]
Common Mistakes:
Passing port and namespace as separate arguments
Using multiple @WebSocketGateway decorators
Mixing argument types and order incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.