0
0
NestJSframework~20 mins

WebSocket guards and pipes in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a WebSocket guard denies access?
In a NestJS WebSocket gateway, if a guard returns false, what is the immediate effect on the client connection or message handling?
AThe client connection is immediately closed by the server.
BThe server throws an exception and disconnects the client.
CThe message is ignored and not processed, but the connection stays open.
DThe guard result is ignored and the message is processed anyway.
Attempts:
2 left
💡 Hint
Think about how guards control message flow without forcibly closing connections.
📝 Syntax
intermediate
2:00remaining
Identify the correct way to apply a pipe to a WebSocket message handler
Which of the following code snippets correctly applies a pipe to transform incoming WebSocket message data in NestJS?
NestJS
import { SubscribeMessage, WebSocketGateway, MessageBody } from '@nestjs/websockets';
import { UsePipes, ValidationPipe } from '@nestjs/common';

@WebSocketGateway()
export class ChatGateway {
  @SubscribeMessage('message')
  @UsePipes(new ValidationPipe())
  handleMessage(@MessageBody() data: any) {
    return data;
  }
}
A@UsePipes(new ValidationPipe()) placed above the @SubscribeMessage decorator.
B@UsePipes(new ValidationPipe()) placed below the @SubscribeMessage decorator and above the method.
CPassing the pipe instance directly as a parameter to @SubscribeMessage decorator.
DApplying the pipe inside the method body manually.
Attempts:
2 left
💡 Hint
Look at the decorator order NestJS expects for pipes on handlers.
🔧 Debug
advanced
2:00remaining
Why does this WebSocket guard not block unauthorized users?
Consider this guard code for a NestJS WebSocket gateway: ```typescript import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; @Injectable() export class AuthGuard implements CanActivate { canActivate(context: ExecutionContext): boolean { const client = context.switchToWs().getClient(); if (client.handshake?.auth?.token === 'valid-token') { return true; } return false; } } ``` Why might this guard fail to block unauthorized users?
AThe guard checks the token on the handshake object, but the token is sent in message data instead.
BThe guard returns a boolean instead of a Promise<boolean>.
CThe guard does not throw an exception on failure, so NestJS ignores the false return.
DThe guard uses ExecutionContext instead of WsContext, causing context.switchToWs() to fail.
Attempts:
2 left
💡 Hint
Check where the token is expected versus where the guard looks for it.
state_output
advanced
2:00remaining
What is the output after applying a custom pipe to transform WebSocket message data?
Given this custom pipe and WebSocket handler in NestJS: ```typescript import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class AppendPipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { return value + ' world'; } } @WebSocketGateway() export class HelloGateway { @SubscribeMessage('greet') @UsePipes(AppendPipe) handleGreet(@MessageBody() message: string) { return message; } } ``` If the client sends the message 'hello', what will the server return?
AThrows a runtime error because AppendPipe is not instantiated.
B'hello'
C'helloundefined world'
D'hello world'
Attempts:
2 left
💡 Hint
Consider how pipes are applied and instantiated in NestJS decorators.
🧠 Conceptual
expert
2:00remaining
Which statement about WebSocket guards and pipes in NestJS is true?
Select the one true statement about how guards and pipes work together in NestJS WebSocket gateways.
AGuards run before pipes, so access control happens before data transformation.
BPipes run only if guards throw exceptions, otherwise they are skipped.
CGuards and pipes run simultaneously, so their order does not matter.
DPipes run before guards, so data is transformed before access is checked.
Attempts:
2 left
💡 Hint
Think about the security flow: check access first, then transform data.