> For the complete documentation index, see [llms.txt](https://mominur.gitbook.io/mominur-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mominur.gitbook.io/mominur-notes/system-design/networking/websockets.md).

# WebSockets

আপনি একটি real-time chat অ্যাপ বানাচ্ছেন। একজন ইউজার মেসেজ পাঠানোর সঙ্গে সঙ্গে অন্যজন সেটা দেখবে — **delay ছাড়াই**। আবার ধরুন, আপনি বানাচ্ছেন live stock trading system, যেখানে প্রতি সেকেন্ডে দামে পরিবর্তন হচ্ছে। এখন প্রশ্ন হলো — কীভাবে এই রিয়েল-টাইম communication সম্ভব? এই জায়গাতেই WebSockets আমাদের সুপারহিরো।

***

### WebSockets কী?

**WebSocket** হলো একটা **full-duplex communication protocol**, যা client (যেমন ব্রাউজার) এবং server এর মধ্যে **একটা persistent connection** তৈরি করে রাখে। অর্থাৎ, দুইপাশ থেকেই যখন খুশি ডেটা পাঠানো যায় — **without re-establishing the connection every time**.

***

### Traditional HTTP vs WebSockets

| Feature                 | HTTP                     | WebSockets                       |
| ----------------------- | ------------------------ | -------------------------------- |
| Connection              | Request-Response ভিত্তিক | Persistent connection            |
| Real-time communication | নয়                       | হ্যাঁ                            |
| Latency                 | বেশি                     | কম                               |
| Use cases               | Static websites, APIs    | Live chats, games, notifications |

HTTP তে প্রতিবার ডেটা পেতে হলে client কে request পাঠাতে হয় (pull), কিন্তু WebSockets এ server নিজেই client কে push করে ডেটা দিতে পারে।

***

### Architecture

একটা WebSocket-based system ডিজাইন করার সময় নিচের জিনিসগুলো গুরুত্বপুর্ণ:

#### 1. **Connection Handling**

* WebSocket connection শুরু হয় একটা HTTP handshake দিয়ে।
* এর পরেই সেটা upgrade হয়ে যায় WebSocket protocol-এ।
* এই connection **long-lived**, তাই প্রতি client এর জন্য server কে একটা socket খোলা রাখতে হয়।

উদাহরণ: ১০০,০০০ ইউজার online? তাহলে ১০০,০০০টা connection maintain করতে হবে — এটা করতে হলে scalable architecture লাগবে।

***

#### 2. **Scalability**

WebSocket connection long-lived হওয়ায় traditional web server (যেমন Apache) যথেষ্ট না।

সমাধান:

* Use **event-driven servers** like **Node.js**, **Go**, **Elixir (Phoenix)**।
* Use **WebSocket gateways** বা **load balancer** (e.g., NGINX, AWS API Gateway with WebSocket support)।

***

#### 3. **Message Routing**

Server side এ যেকোনো client যেকোনো client কে মেসেজ পাঠাতে পারে না — একটা **message routing mechanism** দরকার।

Component:

* **Message Broker** (e.g., Redis pub/sub, Kafka) — server গুলোর মধ্যে coordination রাখে।
* **Room/Channel management** — যেমন chat app এ user “Room A” তে মেসেজ দিলে সেটা “Room A” এর সব ইউজার পাবে।

***

#### 4. **Fault Tolerance & Recovery**

* Connection drop হলে client-side এ **reconnect logic** থাকতে হবে।
* Server side এ fallback mechanism থাকতে হবে: “Was this client really disconnected or just slow?”

***

#### 5. **Security**

WebSockets এ ও security ইস্যু থাকে, বিশেষ করে persistent connection থাকার কারণে।

করণীয়:

* Use **WSS (WebSocket Secure)** – HTTPS এর মতোই এনক্রিপ্টেড।
* **Authentication tokens** (JWT, cookie-based) দিয়ে handshake authenticate করা।
* Per-message validation ও rate limiting।

***

### &#x20;উদাহরণ: Real-Time Chat App Design

#### Components:

* Web frontend (React বা Vue)
* Backend server (Node.js with ws library)
* Redis Pub/Sub (message broadcast এর জন্য)
* NGINX (load balancing ও SSL termination)

**Flow:**

1. Client WebSocket connection খোলে।
2. Server connection authenticate করে।
3. Client মেসেজ পাঠায় -> Server -> Redis pub -> Redis sub -> Server -> অন্য clients কে পাঠিয়ে দেয়।

***

### Real-World Use Cases

* **WhatsApp Web** – প্রতি মেসেজ instant যায়।
* **Stock Trading Platform** – real-time price updates।
* **Online Gaming** – smooth multiplayer communication।
* **Collaborative Tools** – Google Docs টাইপ live editing।

***

WebSockets হলো এমন একটা tool, যেটা সঠিকভাবে ডিজাইন করলে system হবে lightning fast এবং responsive। তবে ভুলভাবে ডিজাইন করলে server crush করে বসে থাকবে।

**System Design এ WebSockets তখনই ইউজ করবেন যখন:**

* Low-latency দরকার
* Real-time user interaction লাগবে
* Push-based data flow দরকার

আর মনে রাখবেন, WebSocket implementation টা যতটা না কোডিং-এর ব্যাপার, তার চেয়ে বেশি হলো **architecture** আর **resource optimization** এর খেলা।
