Cơ BảnNetwork iconNetwork

WebSocket khác gì so với HTTP thông thường? Khi nào nên dùng WebSocket?

HTTP là request-response: client luôn phải khởi tạo request trước, server không thể chủ động push data. WebSocket là full-duplex persistent connection: sau khi upgrade từ HTTP (101 Switching Protocols), cả client và server có thể gửi message bất kỳ lúc nào với latency rất thấp (không cần header HTTP cho mỗi message).

WebSocket phù hợp cho: real-time chat, live notifications, collaborative editing (Google Docs), live trading/stock prices, game online, live dashboard.

Không nên dùng WebSocket khi: data không cần real-time (dùng REST polling hoặc Server-Sent Events thay thế), hoặc khi cần cache/CDN (WebSocket không cache được). Thực tế scaling: WebSocket cần sticky sessions hoặc pub/sub broker (Redis Pub/Sub, Socket.IO adapter) khi có nhiều server instance.

Xem toàn bộ Network cùng filter theo level & chủ đề con.

Mở danh sách Network