0
0
NestJSframework~20 mins

Rooms and namespaces in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Rooms & Namespaces Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a client joins a room in a NestJS WebSocket Gateway?

Consider a NestJS WebSocket Gateway where clients can join rooms. What is the effect of calling client.join('room1') inside a message handler?

NestJS
import { WebSocketGateway, SubscribeMessage, MessageBody, ConnectedSocket } from '@nestjs/websockets';
import { Socket } from 'socket.io';

@WebSocketGateway()
export class ChatGateway {
  @SubscribeMessage('joinRoom')
  handleJoinRoom(@MessageBody() room: string, @ConnectedSocket() client: Socket) {
    client.join(room);
  }
}
AThe client is removed from all other rooms automatically when joining a new room.
BThe client is disconnected from the server immediately after joining the room.
CThe client can no longer send messages to the server after joining the room.
DThe client is added to the specified room and will receive messages sent to that room.
Attempts:
2 left
💡 Hint

Think about what rooms do in socket.io and how they affect message delivery.

📝 Syntax
intermediate
2:00remaining
Which code correctly creates a namespace in a NestJS WebSocket Gateway?

In NestJS, how do you define a WebSocket Gateway that listens on a custom namespace /chat?

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

Check the official NestJS documentation for the correct option key to specify a namespace.

🔧 Debug
advanced
2:00remaining
Why does broadcasting to a room not reach clients in NestJS?

Given this code snippet, why might clients in the room not receive the broadcasted message?

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

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

  @SubscribeMessage('sendMessage')
  handleMessage(@ConnectedSocket() client: Socket, message: string) {
    this.server.to('room1').emit('message', message);
  }

  handleConnection(client: Socket) {
    client.join('room1');
  }
}
AThe server property is never initialized, so broadcasting fails silently.
BClients must call join again after sending a message to receive broadcasts.
CThe room name 'room1' is invalid and must be prefixed with a slash.
DThe emit method requires a callback to confirm delivery to the room.
Attempts:
2 left
💡 Hint

Check how the server property is assigned or initialized.

state_output
advanced
2:00remaining
What is the number of clients in a room after multiple joins and leaves?

Assuming clients join and leave rooms as shown, how many clients remain in room42?

NestJS
import { WebSocketGateway, ConnectedSocket, SubscribeMessage } from '@nestjs/websockets';
import { Socket } from 'socket.io';

@WebSocketGateway()
export class RoomGateway {
  @SubscribeMessage('join')
  joinRoom(@ConnectedSocket() client: Socket) {
    client.join('room42');
  }

  @SubscribeMessage('leave')
  leaveRoom(@ConnectedSocket() client: Socket) {
    client.leave('room42');
  }
}

// Scenario:
// Client A joins room42
// Client B joins room42
// Client A leaves room42
// Client C joins room42
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Count clients currently in the room after all join and leave actions.

🧠 Conceptual
expert
2:00remaining
How do namespaces and rooms differ in NestJS WebSocket gateways?

Which statement best describes the difference between namespaces and rooms in NestJS WebSocket gateways?

ANamespaces and rooms are interchangeable terms for grouping clients in NestJS gateways.
BNamespaces are separate communication channels identified by a path, while rooms are groups within a namespace for targeted messaging.
CRooms are used to authenticate clients, namespaces handle message encryption.
DRooms create new WebSocket connections, namespaces only group clients logically without new connections.
Attempts:
2 left
💡 Hint

Think about how socket.io organizes connections and message delivery.