Understanding WebSocket Communication: How Clients and Servers Talk in Real-Time

Yasar ArafathYasar Arafath
3 min read

In the traditional web model, communication between a client and server happens over HTTP. The client makes a request, and the server responds. While this model works well for many use cases, it's inefficient for real-time applications like chat apps, online games, stock tickers, or collaborative tools, where constant updates are needed. This is where WebSockets come into play.

What is a WebSocket?

A WebSocket is a communication protocol that provides full-duplex (two-way) communication over a single, long-lived connection between the client and the server. This allows data to be exchanged in real-time, without the need to repeatedly open and close connections.

WebSocket vs HTTP

FeatureHTTPWebSocket
CommunicationRequest-ResponseFull-duplex (real-time)
ConnectionNew for every requestPersistent
OverheadHigh (headers sent each time)Low (initial handshake only)
Use caseStatic pages, form submissionsReal-time chat, live updates

Step-by-Step: How WebSocket Communication Happens

1. Client Sends WebSocket Handshake Request

The process starts when the client (usually a browser) sends a handshake request to the server. This handshake is an HTTP request that asks the server to "upgrade" the connection to WebSocket.

Here’s what it looks like:

GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13

Key headers:

  • Upgrade: websocket— tells the server that the client wants to use WebSocket.

  • Connection: Upgrade— required for the upgrade to happen.

  • Sec-WebSocket-Key— A Base64-encoded value used by the server to confirm the handshake.

  • Sec-WebSocket-Version— version of the WebSocket protocol the client supports (typically 13).


2. Server Accepts and Responds to the Handshake

If the server supports WebSockets, it responds with an HTTP 101 status code, confirming the protocol switch:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
  • Sec-WebSocket-Accept is generated by combining it Sec-WebSocket-Key with a magic string and then applying SHA-1 + Base64 encoding. This verifies the client request.

Once this response is received, the WebSocket connection is established, and the HTTP connection is upgraded.


3. Full-Duplex Communication Begins

Now the magic happens: both client and server can send messages to each other at any time.

  • Data is sent as frames (text, binary, ping/pong, etc.).

  • No more headers — only the payload, reducing overhead.

  • Messages can be initiated by either side without waiting for a request.

Example (Client sending message):

socket.send("Hello from client!");

Example (Server sending message):

socket.send("Hello from server!");

4. Connection Stays Open Until Closed

Unlike HTTP requests, WebSocket connections remain open until either:

  • The client closes the connection,

  • The server closes it,

  • A network error occurs.

Both sides can close the connection gracefully using:

socket.close();


When to Use WebSockets

Use WebSockets when:

  • You need real-time updates (e.g., chats, notifications, live dashboards).

  • You want low-latency communication.

  • You need two-way communication with frequent data exchange.


Final Thoughts

WebSockets offer a powerful way to build interactive, real-time applications. By maintaining a persistent, full-duplex connection between client and server, they dramatically reduce the overhead and latency of traditional HTTP-based polling techniques.

Whether you're building a multiplayer game or a stock trading app, understanding WebSocket communication gives you a major tool in your developer toolbox.

11
Subscribe to my newsletter

Read articles from Yasar Arafath directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yasar Arafath
Yasar Arafath