
Welcome to Hello Engineer, your weekly guide to becoming a better software engineer! No fluff - pure engineering insights.
You can also checkout : Elasticsearch 101: Part 1
Have you ever noticed how some websites update instantly like chat apps, stock tickers, or multiplayer games without needing to refresh the page? That magical real-time experience is powered by something called WebSockets. Let’s dive in and see how they work and why they matter!
Imagine you're playing an online game, and the leaderboard keeps changing on the server. How does your browser know when to update?
One way is to ask users to refresh the page every few seconds. Or maybe have your browser keep sending requests every minute to check if something’s changed. That’s called polling and yeah, it works, but it’s not really the best way.
Every time the client sends a request, it waits for the server to respond. If there’s no new data, the server still sends back something usually an empty response. It’s like constantly asking, “Anything new?” and hearing, “Nope” over and over again.

What’s the issue?
Polling creates a lot of traffic. Imagine thousands of users asking the server for updates every second — even when there’s nothing new to show. It wastes resources and can slow things down.
To make it a little better, there’s something called long polling. Here, the server doesn’t respond immediately. Instead, it waits until there’s something new to send back. If there’s still no update after a while, it times out and the client tries again.
It’s better than regular polling, but it still needs to open and close connections over and over. This takes up memory and network resources.
Enter WebSockets
WebSockets were designed to solve the limitations of polling and long polling. Instead of repeatedly opening and closing connections, WebSockets allow the client and server to establish a single, persistent connection that stays open. This enables real-time, two-way communication.
To begin, the client starts by sending a regular HTTP request to the server. But this request includes an "Upgrade" header that tells the server:“I’d like to switch this to a WebSocket connection.”
If the server supports WebSockets, it responds with a confirmation. The HTTP connection is then upgraded to a WebSocket, and from that point on, both the client and server can freely send and receive data through this single TCP connection — without needing to reconnect each time.
Typically, a WebSocket Handler is used in the setup. It's a lightweight server-side component that maintains these open connections and manages active users. This handler ensures that real-time communication is possible across multiple users without needing to repeatedly create new connections.
To summarize the process:
The client sends an HTTP request with an upgrade header.
The server acknowledges and upgrades the connection to WebSocket.
Once established, this connection remains open for ongoing two-way data exchange.
Either the client or server can close the connection at any time, ending the communication.
This persistent connection model is what makes WebSockets highly efficient for real-time applications like chat systems, live notifications, online gaming, and more.tion at any time and the server won’t be able to send you anything now.

Why WebSockets Matter
With traditional methods like polling, the connection would close after every response, forcing the client to keep asking the server for updates. This wasn’t very efficient, especially when there was no new data to send — it just created unnecessary load.
With WebSockets, the communication channel remains open. Once the connection is established, both the client and the server can send data whenever they need to, without waiting for a request from the other side. This makes the whole process much faster and more efficient.
For example, in a real-time chat application:
When User A sends a message, it goes to the server.
Instead of User B constantly checking the server for new messages, the server can immediately send the message to User B over the open WebSocket connection.
There’s no delay, no need to reconnect, and no extra overhead — just instant communication.
This model not only reduces server load but also improves the user experience by delivering updates in real time.
If at any point the client or server no longer needs the connection for example, if the user leaves the chat either side can close it. Once closed, no further messages can be sent through that connection.
Why Not Just Use HTTP?
Here's how standard HTTP works:
Client says: "Hey server, got anything for me?"
Server replies: "Here you go!" (or "Nope, nothing right now.")
Then... they hang up.
It’s always request → response → disconnect.
If you want more data, you have to ask again. And again. And again. Not great for real-time updates.

What makes WebSockets special?
Real-time updates: Messages, prices, scores — all get pushed instantly.
Less overhead: One open connection, not thousands of repeated ones.
Two-way communication: Both sides can send data anytime.
It works over a TCP connection and keeps everything fast and responsive.
Where are WebSockets used?
WebSockets are everywhere in modern apps that need to be real-time.
Chat apps like WhatsApp or Messenger use them to send and receive messages quickly.
Stock apps use WebSockets to show live price updates.
Online games rely on it for smooth, instant interactions.
It’s not just for browsers either lots of backend systems use WebSocket-based tools to power things behind the scenes.
When NOT to use WebSockets?
WebSockets are great, but they’re not always the right tool. If you're just loading data once — like fetching a list of users or getting a product page, a regular HTTP request is easier and lighter. It shine when you need updates right away and all the time.
So that’s WebSockets in a nutshell. They let your browser and server keep an open line of communication, sending messages back and forth in real-time. No more asking, no more waiting.
They’re perfect for anything where speed and instant updates matter chats, games, live scores, stock updates, and more.
It’s one of those things that makes the web feel alive. And now you know how it works!
I hope, by now you must have had a clear idea of WebSockets, from its meaning to working you know all of it now.
Loved this deep dive? Hit a like ❤️For more simple explanations, useful insights on coding, system design, and tech trends, Subscribe To My Newsletter! 🚀
If you have any questions or suggestions, leave a comment.
I hope you have a lovely day!
See you next week with more exciting content!
- Scortier
