Skip to main content

WebSocket's bidirectionality

In short

WebSocket is a bidirectional protocol because both the client and the server can initiate sending data at any moment - with no repeated requests, no waiting, and no closing the connection.

1. How this compares to HTTP

PropertyHTTPWebSocket
Who initiates the exchangeOnly the clientBoth client and server
How long the connection livesUntil the request finishesUntil closed manually
Data transferOne-directional (request → response)Two-directional (both can send messages)
Uses TCPYesYes
Suited for real timeNoYes

Example:

  • HTTP: Client → request → server responds → connection closes.
  • WebSocket: Client ↔ server: both can continuously send messages to each other at any time.

2. Why this is technically possible

WebSocket is built on top of a single TCP connection that:

  • is established once (via an HTTP "upgrade");
  • stays open;
  • lets both sides send messages (frames) asynchronously.

Messages are sent as frames, small binary chunks:

  • the client can send a Text or Binary frame;
  • the server can respond with its own frames;
  • either side can send them at any moment, even simultaneously.

3. An example of a "live" exchange

Once the connection is established:

javascript
Client"Hello, server!" Server"Hi, client!" Client"Sending coordinates {x:10,y:20}" Server"Coordinates received" Server"To everyone: a player moved"

No new HTTP requests, just a two-way stream of messages. This makes WebSocket ideal for chats, games, trading platforms, notifications, and dashboards.

4. What happens under the hood

  1. The client makes a regular HTTP request with headers:
javascript
Upgrade: websocket Connection: Upgrade
  1. The server responds:
javascript
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade
  1. The connection "switches" from HTTP to WebSocket. It's no longer request-response, it's now a persistent, two-way TCP channel.

5. Visually

javascript
HTTP: Client ----request----> Server Client <----response--- Server (connection closed) WebSocket: Client <===============> Server (one connection, both can write and read)

6. Why this matters

Without bidirectionality, it would be impossible to build:

  • real-time notifications;
  • chats and messengers;
  • data streaming (e.g. exchange prices);
  • online games;
  • state updates without reloading the page.

7. To sum up

WebSocket is considered bidirectional because the server and client can each independently initiate sending data over a single persistent connection, staying connected until one of the sides closes it.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.