WebSockets & Real-Time Communication
Lets a server push data to a client the moment something happens, without waiting to be asked.
What is it?
A normal HTTP request flows one way at a time: the client asks, the server answers, and the exchange is over. That's a poor fit for things that must feel instant and two-way, like a chat message arriving, a live score updating, or a multiplayer game move.
WebSockets open one long-lived connection that both client and server can send messages over at any time, with no new request each time.
Explain like I'm 10
A regular HTTP request is like sending a letter and waiting for the reply before you can say anything else. A WebSocket is like an open phone call: either person can speak the moment they have something to say, without hanging up and redialing.
Examples
A basic WebSocket exchange
// Client
const socket = new WebSocket("wss://chat.example.com");
socket.onmessage = (event) => {
console.log("New message:", event.data);
};
socket.send("Hello from the client!");
// Server (conceptually) can push a message at any time,
// without the client having asked for it first:
// socket.send("New message from another user!");How it works
A WebSocket starts as a normal HTTP request asking to be "upgraded". Once the server agrees, the same connection switches to WebSocket mode and stays open.
From then on, client and server can send messages either way at any time, without the overhead of a new HTTP request per message.
Why does it exist?
Some experiences need the server to tell the client the instant something happens (a message arrives, a price changes, another player moves), instead of the client asking "anything new yet?" over and over. WebSockets make that real-time, two-way communication efficient without constant polling.
When to use it
Use WebSockets for frequent, low-latency, two-way communication: chat apps, live collaborative editing, real-time dashboards, multiplayer games.
When not to use it
For data that changes only now and then, or when checking every so often is fine, an always-open connection is unneeded overhead. A regular HTTP request (or an occasional poll) is simpler and doesn't make the server hold a connection open per client.
Common mistakes
Using WebSockets for data that barely changes, adding real infrastructure cost (each open connection uses server resources) for little benefit.
Forgetting a load balancer needs special configuration to keep a client's WebSocket on the same server for the connection's whole life.
Not handling reconnection. Connections drop (network hiccup, server restart), so a robust client must detect that and reconnect.
Practice exercises
- Easy:
Explain in your own words why a chat app benefits from WebSockets more than a news website does.
- Medium:
Describe what 'polling' is, and how it compares to WebSockets for near-real-time updates.
- Hard:
Explain why a load balancer routing WebSocket traffic must behave differently from one routing regular HTTP requests.
Interview questions
What problem do WebSockets solve that plain HTTP doesn't?
The server can push data to the client at any time over one long-lived connection, so the client doesn't have to keep asking for updates.
How does a WebSocket connection get established?
It starts as a normal HTTP request asking to 'upgrade'. Once the server agrees, the same connection switches to WebSocket mode and stays open.
When would a WebSocket be overkill?
When data changes rarely or the client doesn't need instant updates. A normal HTTP request (or occasional polling) is simpler and avoids holding a connection open.