WebSockets

A communication protocol providing full-duplex communication channels over a single TCP connection.

WebSockets represent a significant shift in web architecture by allowing servers to 'push' data to clients without being first requested. This bi-directional capability is essential for modern, reactive web applications. While traditionally browsers were limited to half-duplex requests, WebSockets enable a single TCP connection to handle simultaneous data flow in both directions.

        graph LR
  Center["WebSockets"]:::main
  Rel_websocket["websocket"]:::related -.-> Center
  click Rel_websocket "/terms/websocket"
  Rel_http["http"]:::related -.-> Center
  click Rel_http "/terms/http"
  classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
  classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
  classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
  classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
  linkStyle default stroke:#4b5563,stroke-width:2px;

      

🧠 Knowledge Check

1 / 1

🧒 Explain Like I'm 5

If [HTTP](/en/terms/http) is like sending a text message and waiting for a reply, [WebSockets](/en/terms/websockets) are like a phone call. The line stays open, and both people can talk to each other at any time. This is why WebSockets are used for things like live sports scores or chat apps.

🤓 Expert Deep Dive

WebSockets (RFC 6455) provide a stateful alternative to the stateless HTTP model. The connection lifecycle begins with a 'Switching Protocols' response (HTTP 101) after an Upgrade handshake. This eliminates the overhead of HTTP headers for every message, drastically reducing latency for real-time applications. WSS (WebSocket Secure) ensures data integrity via TLS/SSL. Scaling WebSockets requires architectural considerations such as load balancing with sticky sessions and using pub/sub mechanisms (e.g., Redis) to handle cross-server message propagation. It is the industry standard for financial order books, chat systems, and live sports updates.

❓ Frequently Asked Questions

Is WebSocket better than HTTP?

For real-time data, yes. For static content or standard web pages, HTTP is more efficient.

Does WebSocket use a port?

It typically uses port 80 for WS and 443 for WSS, shares with HTTP.

📚 Sources