Fundamentals 8 min read

Why WebSocket Is Essential for Real‑Time Web Applications

This article explains the WebSocket protocol, its full‑duplex persistent connection, key features, why it outperforms HTTP for real‑time scenarios, and provides detailed handshake examples for both client and server implementations.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Why WebSocket Is Essential for Real‑Time Web Applications

Hello, I'm Xiao Jiang.

With the development of technology, real‑time information is needed in many scenarios such as remote collaboration, epidemic monitoring, social messaging, multiplayer games, stock quotes, live sports, audio/video chat, video conferencing, online education, etc., all of which can leverage WebSocket TCP connections to make data fly.

WebSocket is a full‑duplex communication protocol introduced in HTML5, built on top of TCP, providing a persistent connection between browser and server, unlike the non‑persistent HTTP.

It enables both client‑initiated requests and server‑initiated pushes, allowing true bidirectional communication.

Other characteristics include:

Built on TCP, making server implementation relatively easy. Good compatibility with HTTP; default ports 80 and 443; handshake uses HTTP, so it can pass through proxies. Lightweight data format, low overhead, efficient communication. Supports both text and binary data. No same‑origin restriction; client can communicate with any server. Protocol identifier is ws (or wss for encrypted), and the server address is a URL.

Example URLs:

<code>ws://xxx.ayunw.cn:80/some/path
wss://xxx.ayunw.cn:443/some/path</code>

Clients are not limited to browsers; any socket implementing ws/wss can communicate with the server.

Why do we need WebSocket?

In traditional web architecture, connections are handled by HTTP/1.0 or HTTP/1.1. HTTP is a request‑response protocol; each request is independent and stateless, leading to redundant information (e.g., cookies) and inefficiency as interaction grows.

HTTP is half‑duplex, allowing data flow in only one direction at a time, and only the client can initiate communication, preventing the server from proactively notifying the client of state changes.

Various approaches have been tried to achieve near‑real‑time communication:

Polling: periodic requests, imprecise and wasteful. Long polling: holds the connection until the server has data, but consumes resources and lacks standardization. Streaming: server keeps an open response stream, but can be blocked by proxies and adds latency.

These methods still involve HTTP overhead and increase server load.

What is the WebSocket protocol?

WebSocket is a new protocol that uses an HTTP handshake for compatibility, then upgrades to a full‑duplex, single‑socket connection, solving the real‑time communication limitations of HTTP.

A typical WebSocket handshake:

<code>GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com</code>

The core upgrade headers are:

<code>Upgrade: websocket
Connection: Upgrade</code>

Server response:

<code>HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat</code>

After this, HTTP's role ends and communication proceeds entirely via the WebSocket protocol.

real-timeTCPWebSocketprotocolfull-duplexhandshake
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.