Part 3 · আর্কিটেকচার 📖 ১৩ মিনিট পড়া 📝 ২০টি কুইজ

Long Polling, WebSockets, SSE

Real-time data — তিনটি ভিন্ন approach।

📝 কুইজে যান

Facebook chat-এ কেউ আপনাকে message পাঠালো — মুহূর্তেই notification আসে। Stock price live update হয় চোখের সামনে। Cricket score auto-refresh হয়। কীভাবে server থেকে client-এ data push করা যায়? Traditional HTTP request-response model-এ — নয়। তাই কয়েকটি real-time technique।

Problem: HTTP-এর সীমাবদ্ধতা

HTTP request-response — client জিজ্ঞেস করে, server উত্তর দেয়। Server সাধারণত client-কে initiate করতে পারে না। কিন্তু chat, live score, notification — এসব real-time।

Short Polling (Baseline)

Client প্রতি কয়েক সেকেন্ডে server-কে জিজ্ঞেস করে: "নতুন data আছে?"

Client → Server: any new data? Server → Client: no [wait 5 seconds] Client → Server: any new data? Server → Client: yes! here's data
  • সরল কিন্তু wasteful — অনেক empty request।
  • Latency: polling interval।
  • "Real-time" না।

Long Polling

Client request করে; server data না থাকলে — connection open রেখে wait করে। Data এলে respond। Client immediately new request করে।

Client → Server: any new data? Server: ...waits (data না হলে hold)... Server → Client: data arrived! Client → Server: any new data? (immediately) Server: ...waits...

সুবিধা

  • Server-initiated push-এর illusion।
  • HTTP-native — কোনো special protocol না।
  • Firewall-friendly।
  • Browser-এ work করে সবখানে।

অসুবিধা

  • প্রতি message-এ HTTP overhead।
  • Connection holding-এ server resource।
  • Bidirectional না (only server → client)।
  • Header-heavy।

Use case

  • Notification
  • Comment update
  • Simple chat (legacy)

WebSockets

Full-duplex bidirectional persistent connection। HTTP দিয়ে handshake, তারপর upgrade WebSocket protocol-এ।

Client → Server: HTTP Upgrade: websocket Server → Client: 101 Switching Protocols [Persistent connection established] Client ←→ Server: send messages anytime [Connection closes when done]

সুবিধা

  • True real-time bidirectional।
  • Low overhead — frame-based protocol।
  • Persistent connection।
  • Both side initiate।
  • Binary বা text data।

অসুবিধা

  • Connection scaling complex।
  • Stateful — server-এ state।
  • Auto-reconnect logic দরকার।
  • Some firewall block।
  • HTTP caching nei।

Use case

  • Live chat (WhatsApp web, Slack)।
  • Multiplayer games।
  • Collaborative editing (Google Docs)।
  • Live trading platform।
  • Real-time dashboard।

Server-Sent Events (SSE)

HTTP-based unidirectional stream from server to client। One-way push।

Client → Server: GET /events (Accept: text/event-stream) Server → Client: data: msg1\\n\\n Server → Client: data: msg2\\n\\n [Connection persists] Server → Client: data: msg3\\n\\n

সুবিধা

  • Pure HTTP — proxy, firewall friendly।
  • Auto-reconnection built-in।
  • Last-Event-ID — replay missed events।
  • Browser EventSource API simple।
  • Server simple — no protocol upgrade।

অসুবিধা

  • One-way only (server → client)।
  • Text-only typically।
  • Some browser-এ connection limit (HTTP/1.1: 6 per origin)।
  • HTTP/2 দিয়ে এই limit gone।

Use case

  • Live news feed।
  • Sports score।
  • Notification feed।
  • Stock ticker (one-way)।
  • Server log stream।
  • Build status update।

তুলনা

Long Polling

  • HTTP request-response cycle
  • Server hold-then-respond
  • Server → Client (one-way effectively)
  • HTTP-friendly
  • Higher overhead

WebSockets

  • Persistent TCP connection
  • Full-duplex
  • Both directions
  • Low overhead
  • Stateful

SSE

  • HTTP-based stream
  • One-way (server → client)
  • Auto-reconnect
  • Simple
  • Text-focused

কখন কোনটা?

Long Polling

Legacy compatibility, simple notification, occasional events।

WebSockets

Bi-directional real-time — chat, games, collaborative tools।

SSE

One-way push — news feed, score, notifications, simpler than WebSocket।

WebHook

Server-to-server callbacks (different — async push from one server to another)।

বাস্তব উদাহরণ

  • Slack/Discord: WebSocket (chat)।
  • Twitter timeline updates: SSE / Long polling।
  • Online games (multiplayer): WebSocket।
  • Stock trading: WebSocket।
  • Server logs (kubectl logs): Effectively SSE।
  • GitHub Actions live log: SSE/WebSocket।

Scaling Real-time

  • Sticky session — same client same server।
  • Pub-Sub (Redis, Kafka) — broadcast across servers।
  • Connection limit per server — million-connection issue।
  • Specialized: Centrifugo, SignalR, Socket.IO।
  • WhatsApp/Erlang — 2 million concurrent per server।

সাধারণ ভুল ধারণা

  1. "WebSocket always best": Use case-অনুযায়ী — SSE simpler one-way-এ।
  2. "SSE deprecated": না — modern browser-এ standard।
  3. "Long polling slow": Reasonable, কিন্তু overhead বেশি।
  4. "WebSocket = HTTP": HTTP দিয়ে শুধু handshake; data ভিন্ন protocol।

Best Practices

  • One-way push: SSE (simpler)।
  • Bidirectional: WebSocket।
  • Reconnection logic — connection drop স্বাভাবিক।
  • Heartbeat/ping — connection alive check।
  • Authentication — initial handshake-এ।
  • Sticky session + pub-sub backplane scaling-এ।
  • Connection limit monitoring।

📌 চ্যাপ্টার সারমর্ম

  • Short polling → Long polling → WebSocket → SSE evolution।
  • WebSocket: bidirectional persistent — chat, games।
  • SSE: one-way HTTP stream — news, scores।
  • Long polling: legacy fallback।
  • Scaling: sticky session + pub-sub backplane।