0
0
NestJSframework~30 mins

Rooms and namespaces in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Rooms and Namespaces in NestJS WebSocket Gateway
📖 Scenario: You are building a simple chat server using NestJS WebSocket Gateway. The server should support multiple chat rooms and namespaces so users can join specific rooms and namespaces to chat.
🎯 Goal: Create a NestJS WebSocket Gateway that supports two namespaces: /sports and /music. Each namespace should allow clients to join rooms named room1 or room2. When a client sends a message, it should be broadcast only to other clients in the same room and namespace.
📋 What You'll Learn
Create a WebSocket Gateway with two namespaces: '/sports' and '/music'.
Define a method to handle clients joining a room named 'room1' or 'room2'.
Broadcast messages only to clients in the same room and namespace.
Use NestJS decorators and patterns for WebSocket gateways.
💡 Why This Matters
🌍 Real World
Chat applications often need to separate users into different rooms and namespaces to organize conversations by topic or group.
💼 Career
Understanding how to use rooms and namespaces in WebSocket gateways is essential for building scalable real-time applications in NestJS, a popular backend framework.
Progress0 / 4 steps
1
Create the WebSocket Gateway with namespaces
Create a NestJS WebSocket Gateway class called ChatGateway with two namespaces: /sports and /music. Use the @WebSocketGateway decorator with the namespace option for each namespace. Define two classes: SportsGateway and MusicGateway, each decorated with @WebSocketGateway({ namespace: '/sports' }) and @WebSocketGateway({ namespace: '/music' }) respectively.
NestJS
Need a hint?

Use @WebSocketGateway({ namespace: '/sports' }) to create the sports namespace gateway and similarly for music.

2
Add a method to join rooms
In both SportsGateway and MusicGateway, add a method called handleJoinRoom that takes two parameters: client and room. Use client.join(room) to add the client to the specified room. Use the @SubscribeMessage('joinRoom') decorator on this method.
NestJS
Need a hint?

Use @SubscribeMessage('joinRoom') to listen for join room events and call client.join(room) inside the method.

3
Add message broadcasting within rooms
In both SportsGateway and MusicGateway, add a method called handleMessage decorated with @SubscribeMessage('message'). This method takes client and a payload object with room and text. Use client.to(room).emit('message', text) to broadcast the message to all clients in the same room except the sender.
NestJS
Need a hint?

Use client.to(room).emit('message', text) to send messages only to clients in the same room.

4
Export the gateways in a module
Create a NestJS module called ChatModule that imports and exports both SportsGateway and MusicGateway. Use the @Module decorator with providers and exports arrays containing these gateway classes.
NestJS
Need a hint?

Use @Module decorator with providers and exports arrays to include the gateways.