A namespace is a way to create separate communication channels on the same WebSocket server. It helps organize events and clients into groups that do not interfere with each other.
Click to reveal answer
beginner
How do rooms differ from namespaces in NestJS WebSocket?
Rooms are sub-groups inside a namespace. They allow grouping clients within the same namespace to send messages only to those clients in a specific room.
Click to reveal answer
intermediate
How do you join a client to a room in a NestJS WebSocket gateway?
You use the client socket's join method, like client.join('roomName'), to add the client to a specific room.
Click to reveal answer
beginner
What is the benefit of using namespaces in a real-time chat app?
Namespaces let you separate chat channels or features so messages and events don't mix. For example, you can have a namespace for public chat and another for private chats.
Click to reveal answer
intermediate
How do you define a namespace in a NestJS WebSocket gateway?
You add the @WebSocketGateway decorator with a namespace option, like @WebSocketGateway({ namespace: '/chat' }), to create a gateway for that namespace.
Click to reveal answer
In NestJS WebSocket, what does a room do?
ADefines a new namespace
BCreates a new WebSocket server
CGroups clients within a namespace to send targeted messages
DStores client data permanently
✗ Incorrect
Rooms group clients inside a namespace so you can send messages only to clients in that room.
How do you specify a namespace in a NestJS WebSocket gateway?
AUsing the @WebSocketGateway decorator with a namespace option
BBy calling client.join('namespace')
CBy creating a new NestJS module
DBy setting a global variable
✗ Incorrect
Namespaces are defined by passing the namespace option to the @WebSocketGateway decorator.
Which method adds a client to a room in NestJS WebSocket?
Aclient.join('roomName')
Bclient.connect('roomName')
Cclient.enter('roomName')
Dclient.subscribe('roomName')
✗ Incorrect
The join method adds a client socket to a specific room.
Why use namespaces in a WebSocket app?
ATo encrypt messages
BTo speed up the server
CTo store user data
DTo separate communication channels and avoid event conflicts
✗ Incorrect
Namespaces separate channels so events and messages don't mix between different parts of the app.
Can a client be in multiple rooms within the same namespace?
ANo, only one room per client
BYes, a client can join multiple rooms
COnly if the rooms have the same name
DOnly if the client reconnects
✗ Incorrect
Clients can join multiple rooms to receive messages from different groups.
Explain how namespaces and rooms work together in NestJS WebSocket gateways.
Think of namespaces as floors in a building and rooms as rooms on each floor.
You got /5 concepts.
Describe how you would implement a chat app with multiple chat rooms using NestJS WebSocket namespaces and rooms.
Consider how to organize users and messages so only the right people get the right messages.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of namespaces in a NestJS WebSocket application?
easy
A. To separate communication channels for different parts of the app
B. To store user data persistently
C. To handle HTTP requests
D. To manage database connections
Solution
Step 1: Understand namespaces role
Namespaces create separate channels so different parts of the app communicate independently.
Step 2: Compare other options
Storing data, handling HTTP, or managing databases are unrelated to namespaces in WebSocket context.
Final Answer:
To separate communication channels for different parts of the app -> Option A
Quick Check:
Namespaces = separate channels [OK]
Hint: Namespaces isolate communication paths in WebSocket apps [OK]
Common Mistakes:
Confusing namespaces with database or HTTP handling
Thinking namespaces store data
Mixing namespaces with rooms
2. Which of the following is the correct way to create a namespace in a NestJS WebSocket gateway?
easy
A. @WebSocketGateway({ room: '/chat' })
B. @WebSocketGateway({ namespace: '/chat' })
C. @WebSocketGateway(namespace: '/chat')
D. @WebSocketGateway({ path: '/chat' })
Solution
Step 1: Recall NestJS namespace syntax
Namespaces are set using the options object with the key 'namespace'.
Step 2: Check each option
@WebSocketGateway({ namespace: '/chat' }) uses correct syntax with an options object and 'namespace' key. @WebSocketGateway({ room: '/chat' }) uses incorrect key 'room'. @WebSocketGateway(namespace: '/chat') uses wrong syntax without braces. @WebSocketGateway({ path: '/chat' }) uses 'path' which is unrelated.
Final Answer:
@WebSocketGateway({ namespace: '/chat' }) -> Option B
Quick Check:
Namespace option requires object with 'namespace' key [OK]
Hint: Use @WebSocketGateway({ namespace: '...' }) to set namespaces [OK]
Common Mistakes:
Using 'room' instead of 'namespace'
Using 'path' instead of 'namespace'
Omitting braces for options object
3. Given this NestJS code snippet inside a WebSocket gateway, what will be the output when a client joins the room 'room1' and the server emits to that room?
A. Only clients in 'room1' receive the 'message' event with 'Hello Room1'
B. All clients connected to the namespace receive the message
C. No clients receive the message because rooms are not supported
D. Only the client who sent the message receives it
Solution
Step 1: Understand room targeting
Using to('room1') sends the event only to clients in that room.
Step 2: Analyze other options
All clients connected to the namespace receive the message is incorrect because it targets all clients, not just the room. No clients receive the message because rooms are not supported is false; rooms are supported. Only the client who sent the message receives it is wrong because emit targets room, not sender only.
Final Answer:
Only clients in 'room1' receive the 'message' event with 'Hello Room1' -> Option A
Quick Check:
Emit to room = message only to that room [OK]
Hint: Emit with .to(room) sends only to that room's clients [OK]
Common Mistakes:
Thinking emit sends to all clients
Confusing rooms with namespaces
Assuming sender always receives message
4. Identify the error in this NestJS WebSocket gateway code snippet:
A. The emit method should be called on this.server, not client
B. The event name 'joinRoom' is invalid
C. Missing namespace declaration in the gateway
D. The client object does not have a join method
Solution
Step 1: Check client object methods
The client parameter should be typed as Socket (from socket.io). Typing as any lacks type safety, intellisense, and TypeScript checks for methods like join().
Step 2: Verify other options
Event name 'joinRoom' is valid as a string. Namespace is optional. Emitting to client is valid; client.emit works.
Final Answer:
The client object does not have a join method -> Option D
Quick Check:
client must be typed as Socket with join() method [OK]
Hint: Type client as Socket to use join() safely [OK]
Common Mistakes:
Assuming client always has join method without typing
Confusing event names with invalid strings
Thinking emit must be on server, not client
5. You want to create a chat app with two namespaces: '/public' and '/private'. Inside '/private', users join rooms named after their user IDs. Which approach correctly sets up namespaces and rooms in NestJS?
hard
A. Use @WebSocketGateway() once and manage namespaces manually inside event handlers.
B. Create one gateway without namespaces and use client.join('/public') or client.join('/private') to simulate namespaces.
C. Create two gateways with @WebSocketGateway({ namespace: '/public' }) and @WebSocketGateway({ namespace: '/private' }). In the private gateway, use client.join(userId) to add users to their rooms.
D. Create two gateways but assign rooms globally without joining clients to rooms.
Solution
Step 1: Understand namespaces and rooms setup
Namespaces are best created as separate gateways with the namespace option. Rooms are groups inside namespaces.
Step 2: Analyze options
Create two gateways with @WebSocketGateway({ namespace: '/public' }) and @WebSocketGateway({ namespace: '/private' }). In the private gateway, use client.join(userId) to add users to their rooms. correctly creates two gateways with namespaces and uses client.join(userId) for rooms. Create one gateway without namespaces and use client.join('/public') or client.join('/private') to simulate namespaces. misuses rooms to simulate namespaces, which is incorrect. Use @WebSocketGateway() once and manage namespaces manually inside event handlers. lacks namespaces setup. Create two gateways but assign rooms globally without joining clients to rooms. creates gateways but does not join clients to rooms, so rooms won't work.
Final Answer:
Create two gateways with @WebSocketGateway({ namespace: '/public' }) and @WebSocketGateway({ namespace: '/private' }). In the private gateway, use client.join(userId) to add users to their rooms. -> Option C