What if your app could talk instantly without waiting or refreshing?
Why WebSocket for real-time communication in HLD? - Purpose & Use Cases
Imagine you want to chat with a friend online, but every time you send a message, you have to refresh the page to see if they replied. This back-and-forth refreshing feels slow and frustrating.
Manually refreshing or repeatedly asking the server for updates wastes time and resources. It causes delays, uses more internet data, and makes conversations feel unnatural and laggy.
WebSocket creates a direct, open line between your device and the server. This lets messages flow instantly both ways without waiting or refreshing, making real-time chat smooth and fast.
setInterval(() => fetch('/messages').then(showMessages), 5000);
const socket = new WebSocket('wss://server'); socket.onmessage = e => showMessage(e.data);It enables instant, two-way communication that feels as natural as talking face-to-face.
Online games use WebSocket to instantly share player moves so everyone sees the action live without delays.
Manual polling is slow and wastes resources.
WebSocket keeps a live connection for instant updates.
This makes real-time apps like chat and games smooth and responsive.
Practice
WebSocket over traditional HTTP for real-time communication?Solution
Step 1: Understand WebSocket connection behavior
WebSocket establishes a persistent connection between client and server, unlike HTTP which opens and closes connections per request.Step 2: Identify the key benefit for real-time apps
This persistent connection allows instant two-way communication, ideal for real-time updates.Final Answer:
It keeps a persistent connection open for instant two-way data exchange. -> Option CQuick Check:
Persistent connection = Instant two-way communication [OK]
- Confusing WebSocket with HTTP polling
- Assuming WebSocket compresses all messages automatically
- Thinking WebSocket works without server support
Solution
Step 1: Recall WebSocket URL scheme
WebSocket URLs start withws://orwss://for secure connections, nothttp://.Step 2: Identify correct constructor usage
The WebSocket object is created withnew WebSocket(url), no.connect()or.open()methods exist.Final Answer:
const ws = new WebSocket('ws://example.com/socket'); -> Option BQuick Check:
WebSocket uses ws:// + new WebSocket() [OK]
- Using http:// instead of ws://
- Calling non-existent connect() or open() methods
- Omitting the new keyword
onMessage(client, message) {
broadcastToAllClients(message);
}What will happen if 1000 clients send messages simultaneously?
Solution
Step 1: Analyze server load with simultaneous messages
When many clients send messages at once, the server must process and broadcast each, increasing CPU and network load.Step 2: Understand server resource limits
Without scaling or message queuing, the server can get overwhelmed, causing delays or dropped connections.Final Answer:
Server may become overwhelmed, causing message delays or dropped connections. -> Option AQuick Check:
High load without scaling = delays/drops [OK]
- Assuming instant broadcast without delay
- Thinking clients queue messages automatically
- Believing only first message is processed
ws.on('message', (msg) => {
ws.send(msg);
});Why might this cause problems in a multi-client chat application?
Solution
Step 1: Understand the code behavior
The code listens for a message from a client and sends the same message back only to that client (ws.send(msg)).Step 2: Identify issue in multi-client context
Other connected clients do not receive the message, so chat messages are not broadcasted properly.Final Answer:
It only sends the message back to the sender, not to other clients. -> Option DQuick Check:
ws.send(msg) sends only to sender [OK]
- Assuming ws.send broadcasts to all clients
- Thinking event name 'message' is incorrect
- Believing connection closes after send
Solution
Step 1: Understand scaling challenges with many clients
A single server can become a bottleneck when handling thousands of WebSocket connections and broadcasting messages.Step 2: Identify role of message broker
A message broker like Redis Pub/Sub can distribute messages efficiently across multiple WebSocket servers, balancing load and improving scalability.Final Answer:
A message broker (e.g., Redis Pub/Sub) between WebSocket servers and clients. -> Option AQuick Check:
Message broker enables scalable message distribution [OK]
- Relying on a single server for all connections
- Switching to HTTP long polling loses real-time benefits
- Storing messages in DB adds latency, not scaling
Ready to prove it?
Pass the test and this pattern moves unknown
