WebSocket provides full-duplex communication over a single TCP connection. Understanding protocol-level patterns — ping/pong keepalive, subprotocol negotiation, binary vs text frames, exponential backoff reconnection, and multi-server message ordering — is essential for building reliable real-time applications.
0 / 5 completed
1 / 5
A client connects to a WebSocket server and the connection drops unexpectedly. What is the standard pattern for handling reconnection?
WebSocket reconnection should use exponential backoff: start with a short delay (e.g., 1s), double it on each failure up to a maximum (e.g., 30s), and add jitter (random offset) to prevent all clients from reconnecting simultaneously after a server restart.
2 / 5
What is the purpose of WebSocket ping/pong frames at the protocol level?
WebSocket ping/pong are control frames defined in RFC 6455. The server sends a ping frame, and the client must respond with a pong. This detects half-open connections (where TCP is broken but not yet timed out) and keeps connections alive through NAT gateways that close idle connections.
3 / 5
A developer implements a WebSocket subprotocol by setting the Sec-WebSocket-Protocol header. What does this header negotiate?
The Sec-WebSocket-Protocol header negotiates an application-level subprotocol between client and server. Examples include STOMP (for message queuing), MQTT (for IoT), or custom protocols. Both client and server must agree on the subprotocol during the WebSocket handshake.
4 / 5
When should a developer use WebSocket's binary message type instead of text messages?
WebSocket's binary frames should be used for truly binary content: images, audio streams, compiled Protocol Buffer payloads, or MessagePack-encoded data. Binary frames avoid UTF-8 validation overhead on text frames. JSON over text frames is fine for structured data, but binary encoding like MessagePack over binary frames is more efficient.
5 / 5
A chat application uses WebSocket and needs to handle message ordering across multiple server instances. What challenge does this introduce?
While a single TCP WebSocket connection maintains message order, in a multi-server deployment, users connected to different servers receive messages in different orders. Solutions include a message broker (Redis Pub/Sub, Kafka) to distribute messages with timestamps or sequence numbers, enabling clients to reorder messages by sequence.