What is WebSocket?
WebSocket is an application-layer protocol that provides a persistent, two-way connection between a client and a server.
In short: WebSocket lets a client and a server "talk" in real time.
Why WebSocket is needed
Plain HTTP works like this:
- the client sends a request
- the server responds
- the connection ends
This is inconvenient when you need to:
- get updates instantly
- exchange messages continuously
- avoid waiting for a new request from the client
WebSocket solves this problem.
The core idea of WebSocket
The connection is established once and stays open.
After that:
- the client can send data to the server at any moment
- the server can send data to the client without being asked
This is called a two-way (full-duplex) connection.
How WebSocket works (simplified)
- The client connects to the server over HTTP
- An "upgrade" happens - a switch from HTTP to WebSocket
- A persistent connection is established
- The client and server freely exchange messages
No repeated requests or waiting.
Where WebSocket is used
Typical examples:
- online chats
- messengers
- stock quotes
- online games
- real-time notifications
- collaborative editors (Google Docs and similar tools)
Anywhere minimal latency matters.
WebSocket vs HTTP (in short)
- HTTP
- request → response
- no persistent connection
- the server cannot initiate sending data on its own
- WebSocket
- persistent connection
- the server can send data on its own
- suits real-time use cases
A real-life example
HTTP is like corresponding by email: you write → you wait → you get a reply.
WebSocket is like a phone call or a chat: you can talk at any moment and get a reply right away.
Short answer for the interview
Remember this phrasing:
WebSocket is an application-layer protocol that establishes a persistent, two-way connection between a client and a server and is used to exchange data in real time.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.