Technical
WebSocket
WebSocket is a persistent two-way connection between browser and server, opened by an HTTP upgrade and then carrying messages in both directions without new requests.
How it is measured
In DevTools the WS tab shows the `101 Switching Protocols` handshake and then the frames in each direction with sizes and timing. On the server, count open connections, messages per second, and ping round-trip time. Proxies close idle connections after about 60 seconds without traffic.
Watch the reconnect rate and memory per connection, since each one holds a socket and buffers.
Worked example
A chat widget on a support site keeps 1,200 sockets open at peak. nginx's default `proxy_read_timeout` of 60 seconds closes silent ones, and 300 clients reconnect every minute, spiking CPU. A ping every 25 seconds drops reconnects to 6 a minute.
A redeploy kills all 1,200 at once and they reconnect together. Adding a random backoff of 1 to 10 seconds smooths out the reconnect storm.
How it differs
A WebSocket is two-way and has its own framing. Server-sent events are one-way text over plain HTTP with automatic reconnect. Use SSE when only the server needs to talk, since a WebSocket costs more to run and secure.
Common errors
Sending no heartbeats. Reconnecting with no backoff. Trusting origin blindly instead of checking `Origin` on the handshake. Sending unbounded messages. Skipping authentication at upgrade. Ignoring mobile network changes.
In practice
Use a WebSocket only if the client must send often. Otherwise SSE or polling is simpler. Add heartbeats, backoff, origin checks, and message size limits.