WebSocket
Định nghĩa
WebSocket là protocol giữ một kết nối persistent full-duplex để client và server gửi message cho nhau bất cứ lúc nào.
Cách hiểu bằng lời của tôi
WebSocket đáng dùng khi cả hai phía đều thật sự nói chuyện liên tục: multiplayer game, collaborative editor, trading interface. Nếu chỉ server đẩy update một chiều, Server-Sent Events thường đơn giản hơn.
Trade-off
- Giảm overhead request-response lặp lại.
- Hỗ trợ realtime hai chiều.
- Tốn tài nguyên vì mỗi client giữ connection.
- Cần xử lý reconnect, heartbeat, backpressure, auth và abuse trên connection dài.
- Với payload nhỏ, thường xuyên và lặp lại, Streaming Compression và Delta Update có thể giảm bandwidth đáng kể hơn chỉ scale thêm server.