WebSocket: complete QA guide
WebSocket is a persistent, full-duplex protocol for cases where both client and server need to send data at any time over one long-lived connection. Typical uses include chats, live notifications, collaborative editing, multiplayer games, dashboards, market feeds, device telemetry, presence indicators, and other interactive real-time features.
WebSocket vs HTTP polling vs SSE
| Mechanism | Direction | Connection model | Good fit |
|---|---|---|---|
| HTTP request/response | Client → server, then response | Separate requests | CRUD and ordinary APIs |
| Polling | Client repeatedly asks | Repeated HTTP requests | Simple, infrequent updates |
| Server-Sent Events (SSE) | Server → browser | Long-lived HTTP stream | One-way server push |
| WebSocket | Both directions | Long-lived duplex connection | Interactive low-latency messaging |
Do not choose WebSocket merely because a feature is called real time. If only the server pushes events, SSE can be simpler. If updates are infrequent, normal HTTP or polling can be easier to operate.
URLs: ws:// and wss://
- ws:// — WebSocket without TLS.
- wss:// — WebSocket over TLS; prefer it in production and for sensitive traffic.
- A connection has an opening handshake, a long-lived message phase, and a closing handshake.
Opening handshake
Classic WebSocket starts with an HTTP/1.1 Upgrade handshake:
GET /chat HTTP/1.1
Host: api.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Origin: https://app.example.comSuccessful response:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=101 Switching Protocols is only the connection upgrade. After that, application data travels as WebSocket frames rather than ordinary HTTP request/response pairs. RFC 8441 also defines bootstrapping WebSocket over HTTP/2, so do not assume every deployment internally uses the HTTP/1.1 Upgrade path.
Messages and frames
WebSocket supports:
- text messages — UTF-8, commonly JSON;
- binary messages — application-defined binary data;
- continuation/fragmented frames — one logical message may span multiple frames;
- Ping/Pong control frames — protocol-level liveness;
- Close control frames — graceful closing handshake.
Tests should validate logical application messages rather than depend on one message equaling one network frame.
Connection lifecycle
Browser WebSocket states:
- CONNECTING — handshake in progress.
- OPEN — messages can be sent and received.
- CLOSING — closing handshake in progress.
- CLOSED — connection closed or failed.
Applications usually add authentication, subscriptions or rooms, message schemas, acknowledgements, sequencing, retries, heartbeat policy, and reconnect behavior. WebSocket itself does not automatically provide those application semantics.
Browser client example
const socket = new WebSocket("wss://api.example.com/ws", ["chat.v1"]);
socket.addEventListener("open", () => {
console.log("connected", socket.protocol);
socket.send(JSON.stringify({
type: "subscribe",
roomId: "qa-team"
}));
});
socket.addEventListener("message", (event) => {
const message = JSON.parse(event.data);
console.log("received", message);
});
socket.addEventListener("error", () => {
console.error("WebSocket error");
});
socket.addEventListener("close", (event) => {
console.log({ code: event.code, reason: event.reason, clean: event.wasClean });
});The browser constructor takes a URL and optional subprotocol list. Browser JavaScript does not offer a general API for arbitrary custom handshake headers, so authentication design must account for browser constraints.
Minimal Node.js server example
Using the popular ws package:
import { WebSocketServer } from "ws";
const wss = new WebSocketServer({ port: 8080 });
wss.on("connection", (socket, request) => {
console.log("client connected", request.headers.origin);
socket.send(JSON.stringify({ type: "welcome" }));
socket.on("message", (raw) => {
let message;
try {
message = JSON.parse(raw.toString());
} catch {
socket.close(1003, "Invalid JSON");
return;
}
if (message.type === "ping") {
socket.send(JSON.stringify({ type: "pong", id: message.id }));
return;
}
socket.send(JSON.stringify({ type: "echo", payload: message }));
});
});npm install ws
node server.mjsProduction code must validate input, authenticate the connection, authorize each action, enforce message-size and rate limits, handle malformed data, manage slow consumers and backpressure, and clean up disconnected clients.
Python client example
import asyncio
import json
import websockets
async def main():
async with websockets.connect("ws://localhost:8080") as ws:
await ws.send(json.dumps({"type": "ping", "id": 42}))
response = json.loads(await ws.recv())
assert response == {"type": "pong", "id": 42}
asyncio.run(main())pytest WebSocket example
import json
import pytest
import websockets
@pytest.mark.asyncio
async def test_ping_pong():
async with websockets.connect("ws://localhost:8080") as ws:
await ws.send(json.dumps({"type": "ping", "id": "req-1"}))
message = json.loads(await ws.recv())
assert message["type"] == "pong"
assert message["id"] == "req-1"Do not stop at message received. Assert schema, values, correlation IDs, ordering requirements, permissions, and side effects.
Manual testing
Useful tools:
- Browser DevTools → Network → WS → inspect handshake and Frames/Messages.
- wscat or websocat.
- API clients with WebSocket support.
- Service logs and traces correlated by connection ID or message ID.
npx wscat -c ws://localhost:8080
> {"type":"ping","id":1}What exactly to test
#### 1. Handshake and connection
- valid ws:// and wss:// endpoint;
- expected 101 response for HTTP/1.1 Upgrade;
- Upgrade, Connection, and Sec-WebSocket headers;
- supported and unsupported subprotocol negotiation;
- valid and invalid Origin;
- TLS certificate, hostname, and expiry for wss://;
- proxy and load-balancer behavior;
- connection timeout and refused connection.
#### 2. Message contract
For every message type verify required fields and types, missing and unknown fields, malformed JSON, empty payloads, Unicode, boundary values, oversized messages, text versus binary behavior, schema/version compatibility, acknowledgements, and correct recipient, room, tenant, or subscription.
Example application envelope:
{
"type": "chat.message",
"id": "msg-123",
"correlationId": "req-77",
"timestamp": "2026-08-28T12:00:00Z",
"payload": {
"roomId": "qa-team",
"text": "Hello"
}
}Stable message IDs and correlation IDs make duplicates, retries, and tracing testable.
#### 3. Ordering, duplicates, and concurrency
WebSocket preserves transport ordering on one connection, but distributed applications can still create business-level ordering issues across producers, brokers, retries, rooms, or reconnects. Test rapid sequential messages, multiple concurrent clients, duplicate client messages, reconnect while messages are in flight, fan-out to 10/100/1000 subscribers, and races between acknowledgements and server events.
#### 4. Disconnect and reconnect
Simulate Wi-Fi off/on, mobile-network switching, sleep/wake, server restart or deploy, proxy idle timeout, abrupt TCP loss, and graceful close.
Verify that the client reconnects when intended, uses bounded backoff and jitter rather than a reconnect storm, re-authenticates when required, restores subscriptions, avoids duplicate active sockets and UI events, and has defined behavior for missed and in-flight messages.
let retry = 0;
function connect() {
const ws = new WebSocket("wss://api.example.com/ws");
ws.addEventListener("open", () => {
retry = 0;
});
ws.addEventListener("close", () => {
const delay = Math.min(30000, 1000 * 2 ** retry);
retry += 1;
setTimeout(connect, delay + Math.random() * 500);
});
}
connect();The exact retry policy is application-specific. The important points are bounded exponential backoff, jitter, cancellation when no longer needed, and no duplicate active connections.
#### 5. Heartbeats and idle connections
Ping/Pong control frames can detect dead peers; some applications also use JSON-level heartbeats. Test the expected heartbeat interval, missing Pong response, reverse-proxy or load-balancer idle timeout, false disconnects on slow networks, cleanup of dead connections, and heartbeat overhead at scale.
#### 6. Close codes
| Code | Meaning |
|---|---|
| 1000 | Normal closure |
| 1001 | Going away |
| 1002 | Protocol error |
| 1003 | Unsupported data |
| 1008 | Policy violation |
| 1009 | Message too big |
| 1011 | Unexpected server condition |
1005 and 1006 are reserved status values describing absence or abnormal closure and are not normal Close-frame codes sent by an endpoint.
#### 7. Authentication and authorization
A successful handshake does not mean the socket is authorized forever. Test connection-level and message-level permissions. Possible designs include authenticated cookies, credentials during connection setup where supported, a short-lived URL token with leakage risks, a subprotocol-based scheme, or an authentication message immediately after connection.
Test missing, expired, and revoked credentials; user A subscribing to user B private room; token expiry while the socket is open; role changes during an active connection; reconnect with a stale token; server-side revocation; and tenant isolation.
#### 8. Origin and WebSocket security
CORS is not the WebSocket authorization model. Browser WebSocket handshakes include an Origin header. Validate allowed origins when browser-origin security matters, especially with cookie-authenticated sockets, but never treat Origin validation as a replacement for authentication and authorization.
Also test wss:// for sensitive traffic, cross-site WebSocket hijacking, message-level authorization, injection through text payloads, size/rate limits, connection quotas, sensitive data in URLs or logs, and malformed protocol frames with lower-level tooling when relevant.
#### 9. Performance and scale
WebSocket performance differs from ordinary REST load testing because connections stay open. Model concurrent open connections, connections per second, messages per second in both directions, message-size distribution, fan-out, connection lifetime, reconnect storms, slow consumers/backpressure, memory per connection, CPU, file descriptors, network throughput, event-loop lag, and proxy/load-balancer limits.
A server that handles 10,000 idle sockets may still fail when 10,000 clients each receive 50 messages per second. Test the complete workload, not only the connection count.
Backpressure
The classic browser WebSocket API does not provide built-in backpressure. If messages arrive faster than the application consumes them, buffers and memory can grow. Explicitly test slow-consumer behavior for high-rate streams. MDN also documents WebSocketStream, a Streams-based alternative with backpressure, although support differs from the broadly supported classic WebSocket interface.
WebSocket debugging checklist
- Did the handshake succeed, and what status and headers were returned?
- Is the URL ws:// or wss://, and is TLS valid?
- Which Origin and subprotocol were negotiated?
- Was authentication accepted, and is authorization enforced per action?
- Which exact message type and schema were sent?
- Did the server send an acknowledgement or event?
- Are connection and message correlation IDs present in logs or traces?
- Did ordering, duplication, or reconnect affect the result?
- What close code and reason were observed?
- Is a proxy or load balancer terminating idle sockets?
- Is the client consuming messages slower than they arrive?
- Can the scenario be reproduced with a minimal CLI or script client?
Key takeaways
WebSocket provides persistent bidirectional communication, begins with a handshake (classically HTTP Upgrade → 101), then exchanges text, binary, and control frames over a long-lived connection. Testing must cover handshake, authentication and authorization, Origin, message schema, ordering, duplicates, heartbeat, close codes, reconnect, missed messages, concurrency, slow consumers, and scale.