0
0
NestJSframework~20 mins

WebSocket gateway creation in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Gateway 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 NestJS WebSocket gateway?
Consider this NestJS WebSocket gateway code. What message will the server send to the client immediately after connection?
NestJS
import { WebSocketGateway, OnGatewayConnection, WebSocketServer } from '@nestjs/websockets';
import { Server, Socket } from 'socket.io';

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

  handleConnection(client: Socket) {
    client.emit('welcome', 'Hello Client!');
  }
}
AThe server throws an error because handleConnection is missing a return type.
BThe client receives no message until it sends one first.
CThe client receives a message 'welcome' but with an empty string.
DThe client receives the message 'Hello Client!' immediately after connecting.
Attempts:
2 left
💡 Hint
Look at what the handleConnection method does with the client socket.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a WebSocket gateway listening on port 3001?
You want to create a NestJS WebSocket gateway that listens on port 3001. Which code snippet correctly sets this up?
A
@WebSocketGateway({ port: 3001 })
export class MyGateway {}
B
@WebSocketGateway(3001)
export class MyGateway {}
C
@WebSocketGateway(port=3001)
export class MyGateway {}
D
@WebSocketGateway(port: 3001)
export class MyGateway {}
Attempts:
2 left
💡 Hint
Check the decorator syntax for passing options as an object.
lifecycle
advanced
2:00remaining
What happens if you implement OnGatewayDisconnect but forget to handle the disconnect event?
Given this gateway code, what will be the behavior when a client disconnects?
NestJS
import { WebSocketGateway, OnGatewayDisconnect } from '@nestjs/websockets';
import { Socket } from 'socket.io';

@WebSocketGateway()
export class MyGateway implements OnGatewayDisconnect {
  handleDisconnect(client: Socket) {
    // Intentionally left blank
  }
}
AThe client connection remains open because disconnect is not handled properly.
BThe server throws a runtime error because handleDisconnect is empty.
CThe server logs nothing and silently ignores the disconnect event.
DThe server automatically reconnects the client.
Attempts:
2 left
💡 Hint
Think about what an empty method implementation means for event handling.
🔧 Debug
advanced
2:00remaining
Why does this WebSocket gateway fail to emit messages to all clients?
Review this gateway code. Why does calling this.server.emit('msg', 'hello') not send messages to all connected clients?
NestJS
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';

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

  sendMessage() {
    this.server.emit('msg', 'hello');
  }
}
AThe server property is undefined because @WebSocketServer decorator is missing parentheses.
BThe server property is undefined because the gateway is not instantiated by NestJS.
CThe server property is undefined because the class lacks the OnGatewayInit interface implementation.
DThe server property is undefined because the @WebSocketServer decorator is not imported from the correct package.
Attempts:
2 left
💡 Hint
Consider how NestJS creates and manages gateway instances and their properties.
🧠 Conceptual
expert
3:00remaining
How does NestJS WebSocket gateway handle multiple namespaces?
You want to create two WebSocket gateways in NestJS, each listening on a different namespace: '/chat' and '/news'. How should you configure the gateways to achieve this?
AUse @WebSocketGateway({ namespace: '/chat' }) on one class and @WebSocketGateway({ namespace: '/news' }) on the other.
BUse @WebSocketGateway('/chat') on one class and @WebSocketGateway('/news') on the other.
CUse @WebSocketGateway() on both classes and manually filter namespaces inside handleConnection.
DUse a single gateway with @WebSocketGateway() and handle namespaces via client-side events.
Attempts:
2 left
💡 Hint
Check how the namespace option is passed in the decorator options object.