a day ago
- WebSockets enable two-way communication with a web server, reducing HTTP overhead by keeping the connection open.
- A WebSocket connection starts as HTTP and then upgrades to the WebSocket protocol, avoiding repeated request-response cycles.
- WebSockets allow the server to push unsolicited data to the client, solving a limitation of traditional HTTP.
- They use the same port as the web server (port 80) to help with firewall traversal.
- Browser support is strong except for IE9 and earlier; fallback libraries like Socket.IO can be used for older browsers.
- Practical example: a simple chat server and client implemented in NodeJS using the WebSocket protocol.
- Custom higher-level protocols (e.g., beej-chat-protocol) can be defined using JSON messages with a type and payload.
- Connection establishment involves the client requesting an upgrade; the server can accept or deny based on origin or protocol support.
- Normal communication involves sending and receiving JSON packets, with events for data arrival, closure, and errors.