0
0
NestJSframework~20 mins

Why WebSockets enable real-time features in NestJS - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How do WebSockets maintain a continuous connection?

In NestJS, WebSockets allow a server and client to communicate continuously. How does this connection stay open?

AWebSockets create a single TCP connection that stays open for ongoing two-way communication.
BThe client sends a new HTTP request for every message to keep the connection alive.
CThe server repeatedly closes and reopens connections to simulate real-time updates.
DWebSockets use multiple short-lived HTTP requests to mimic a continuous connection.
Attempts:
2 left
💡 Hint

Think about how a phone call stays open versus sending letters back and forth.

component_behavior
intermediate
2:00remaining
What happens when a NestJS WebSocket Gateway receives a message?

Consider a NestJS WebSocket Gateway that listens for messages. What is the typical behavior when a message arrives?

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

@WebSocketGateway()
export class ChatGateway {
  @SubscribeMessage('message')
  handleMessage(@MessageBody() data: string): string {
    return `Received: ${data}`;
  }
}
AThe server processes the message and sends a response back to the client immediately.
BThe server queues the message but does not send any response.
CThe server ignores the message and waits for the next one.
DThe server closes the connection after receiving the message.
Attempts:
2 left
💡 Hint

Think about how a chat app replies instantly when you send a message.

lifecycle
advanced
2:00remaining
What lifecycle event occurs when a client disconnects from a NestJS WebSocket Gateway?

In NestJS WebSocket Gateway, which lifecycle method is triggered when a client disconnects?

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

@WebSocketGateway()
export class ChatGateway implements OnGatewayDisconnect {
  handleDisconnect(client: any) {
    console.log('Client disconnected');
  }
}
AhandleConnection()
BhandleDisconnect()
CafterInit()
DhandleMessage()
Attempts:
2 left
💡 Hint

Think about the name that suggests a client leaving or disconnecting.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly broadcasts a message to all connected clients in NestJS WebSocket Gateway?

Choose the correct way to send a message to all clients connected to a NestJS WebSocket Gateway.

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

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

  broadcastMessage(message: string) {
    // Fill in the blank
  }
}
Athis.server.sendMessage('message', message);
Bthis.server.send('message', message);
Cthis.server.emit('message', message);
Dthis.server.broadcast('message', message);
Attempts:
2 left
💡 Hint

Look for the method that sends an event to all clients in Socket.IO.

🔧 Debug
expert
3:00remaining
Why does this NestJS WebSocket Gateway fail to receive messages?

Given the following code, why does the server never log incoming messages?

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

@WebSocketGateway()
export class ChatGateway {
  @SubscribeMessage('chat')
  handleChat(@MessageBody() data: string) {
    console.log('Message:', data);
  }
}
AThe handleChat method must return a value to receive messages.
BThe @SubscribeMessage decorator is missing a required second argument.
CThe WebSocketGateway decorator requires a port number to work.
DThe client is sending messages with event name 'message' instead of 'chat'.
Attempts:
2 left
💡 Hint

Check if the event names between client and server match exactly.