Real-time Chat Application
Build a simple real-time chat application demonstrating message broadcasting and private messaging functionality.
What Makes a Chat App Real-time?
Real-time chat applications allow users to send and receive messages instantly, without refreshing their browser.
This is made possible by WebSockets, which provide a persistent, bidirectional communication channel between the client and server.
In this lesson, we'll build a simple chat app with NestJS, demonstrating message broadcasting and private messaging.
Setting Up Our Chat Gateway
In NestJS, a Gateway is a class annotated with @WebSocketGateway() that handles WebSocket connections and messages.
It acts as the entry point for real-time communication. We'll specify a port for our WebSocket server.
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';
@WebSocketGateway(3001, { cors: true }) // Port 3001, enable CORS
export class ChatGateway {
@WebSocketServer() server: Server; // socket.io server instance
// More logic will go here
}