0
0
NestJSframework~20 mins

Gateway decorator in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Gateway Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a client connects to this gateway?

Consider this NestJS WebSocket gateway code. What will be logged when a client connects?

NestJS
import { WebSocketGateway, OnGatewayConnection } from '@nestjs/websockets';

@WebSocketGateway()
export class MyGateway implements OnGatewayConnection {
  handleConnection(client: any, ...args: any[]) {
    console.log('Client connected:', client.id);
  }
}
ALogs 'Client connected:' followed by the client's id
BDoes not log anything because OnGatewayConnection is not implemented correctly
CLogs 'Client connected:' but client.id is undefined
DThrows a runtime error because handleConnection is missing a return type
Attempts:
2 left
💡 Hint

Check how the handleConnection method works in a gateway implementing OnGatewayConnection.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies the Gateway decorator with a namespace?

Choose the correct way to define a WebSocket gateway with the namespace '/chat'.

A
@WebSocketGateway('/chat')
export class ChatGateway {}
B
@WebSocketGateway({ namespace: '/chat' })
export class ChatGateway {}
C
@Gateway('/chat')
export class ChatGateway {}
D
@WebSocketGateway(namespace: '/chat')
export class ChatGateway {}
Attempts:
2 left
💡 Hint

Remember the Gateway decorator accepts an options object for namespace.

lifecycle
advanced
2:00remaining
What happens if you implement OnGatewayDisconnect but forget to add the decorator?

Given this gateway class implementing OnGatewayDisconnect but missing the @WebSocketGateway() decorator, what is the behavior when a client disconnects?

NestJS
import { OnGatewayDisconnect } from '@nestjs/websockets';

export class MyGateway implements OnGatewayDisconnect {
  handleDisconnect(client: any) {
    console.log('Client disconnected:', client.id);
  }
}
AA runtime error occurs because the decorator is missing
BThe disconnect handler is called normally and logs the client id
CThe disconnect handler is never called because the class is not registered as a gateway
DThe server crashes on startup due to missing decorator
Attempts:
2 left
💡 Hint

Think about what the decorator does in NestJS gateways.

🔧 Debug
advanced
2:00remaining
Why does this gateway fail to emit events to clients?

Examine the code below. Why does the sendMessage method not send messages to connected clients?

NestJS
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';

@WebSocketGateway()
export class ChatGateway {
  @WebSocketServer()
  server: Server;

  sendMessage(message: string) {
    this.server.emit('message', message);
  }
}
AThe sendMessage method is never called, so no messages are sent
BThe server property is undefined because the gateway is not instantiated by NestJS
CThe @WebSocketServer decorator is missing parentheses
DThe emit method requires a callback to confirm delivery
Attempts:
2 left
💡 Hint

Check if the method that emits messages is actually used.

🧠 Conceptual
expert
2:00remaining
What is the effect of setting 'transports' option in @WebSocketGateway decorator?

Consider this gateway decorator:

@WebSocketGateway({ transports: ['websocket'] })

What does setting transports to ['websocket'] do?

AIt causes a syntax error because transports is not a valid option
BIt enables both WebSocket and polling transports for clients
CIt disables WebSocket transport and uses only polling
DIt forces clients to connect only using WebSocket protocol, disabling polling fallback
Attempts:
2 left
💡 Hint

Think about how Socket.IO handles transports.