Tech TutorBangla Web Documentation
Portfolio
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • NestJS Documentation21টি অধ্যায়
প্রযুক্তি নির্বাচন
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Introduction
  • Installation
  • Modules
  • Controllers
  • Providers & DI
  • Pipes
  • Guards
  • Interceptors
  • Exception Handling
  • Middleware
  • Database
  • Authentication
  • Config & Env
  • File Upload
  • WebSockets
  • Microservices
  • Swagger
  • Testing
  • Best Practices
  • Project
  • Interview
  1. Docs
  2. NestJS
  3. NestJS WebSockets (Gateway)
NestJSAdvanced

NestJS WebSockets (Gateway)

@WebSocketGateway, events, rooms, real-time — NestJS websocket বাংলায়।

11টি অংশবাংলা ভাষায়

WebSocket bidirectional real-time communication— chat, notification, live updates। NestJS-এ @WebSocketGateway দিয়ে সহজ।

Setup

npm i @nestjs/websockets @nestjs/platform-socket.io socket.io

Chat Gateway

src/chat/chat.gateway.tstypescript
typescript
import {
  ConnectedSocket,
  MessageBody,
  OnGatewayConnection,
  OnGatewayDisconnect,
  SubscribeMessage,
  WebSocketGateway,
  WebSocketServer,
} from "@nestjs/websockets";
import { Server, Socket } from "socket.io";

@WebSocketGateway({ cors: { origin: "*" } })
export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
  @WebSocketServer()
  server: Server;

  handleConnection(client: Socket) {
    console.log("Client connected:", client.id);
  }

  handleDisconnect(client: Socket) {
    console.log("Client disconnected:", client.id);
  }

  @SubscribeMessage("message")
  handleMessage(
    @ConnectedSocket() client: Socket,
    @MessageBody() data: { room: string; text: string },
  ) {
    // room-এ broadcast
    client.to(data.room).emit("message", {
      from: client.id,
      text: data.text,
    });
  }

  @SubscribeMessage("join")
  joinRoom(@ConnectedSocket() client: Socket, @MessageBody() room: string) {
    client.join(room);
    this.server.to(room).emit("system", "A user joined " + room);
  }
}

Module & Client

src/chat/chat.module.tstypescript
typescript
import { Module } from "@nestjs/common";
import { ChatGateway } from "./chat.gateway";

@Module({
  providers: [ChatGateway],
})
export class ChatModule {}
Client side (socket.io-client)javascript
javascript
import { io } from "socket.io-client";

const socket = io("http://localhost:3000");

socket.on("connect", () => {
  console.log("connected:", socket.id);
  socket.emit("join", "room-1");
});

socket.on("message", (data) => {
  console.log(data.from, "says:", data.text);
});

socket.on("system", (msg) => console.log("System:", msg));

// send message
socket.emit("message", {
  room: "room-1",
  text: "হ্যালো রুম!",
});

NestJS থেকে Server-এ emit

// service-এ gateway inject
@Injectable()
export class NotificationService {
  constructor(private readonly chatGateway: ChatGateway) {}

  notifyAll(message: string) {
    this.chatGateway.server.emit("notification", message);
  }

  notifyRoom(room: string, payload: any) {
    this.chatGateway.server.to(room).emit("event", payload);
  }
}
Scalability

একাধিক server-এ websocket চালাতে Redis adapter দরকার (@socket.io/redis-adapter) — এতে socket event shared হয়।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরNestJS File Upload (Multer)পরেরNestJS Microservices
এই পেজে
Tech Tutor

বাংলায় ওয়েব ডেভেলপমেন্ট ডকুমেন্টেশন শিখুন — ধাপে ধাপে।

Homeসব ডকুমেন্টেশনQuick Startআমার নোটস

© 2026 Tech Tutor. বাংলায় শিখুন, বাংলায় গড়ুন।

mdanamolhasan1998@gmail.com