All posts

WebSockets: Because Refreshing the Page Isn't a Feature

June 25, 20263 min read
WebSocketsNode.jsJavaScriptBackend

The Problem

Traditional HTTP is great.

The client asks for something.

The server replies.

Everyone moves on with their lives.

But then you decide to build a chat app.

Or a multiplayer game.

Or a live dashboard.

Suddenly you're making a request every second hoping something changed.

Congratulations. You've reinvented "Is it there yet?" as a backend architecture.

That's where WebSockets save the day.

So... What Exactly Are WebSockets?

A WebSocket creates one persistent connection between the client and server.

Once it's connected, either side can send data whenever they want.

No refreshing.

No constant polling.

No unnecessary requests asking, "Anything new?" every two seconds.

The connection just stays alive.

And honestly, it's a lot less needy than HTTP.

Where You'll Actually Use Them

WebSockets aren't just for chat apps.

They're everywhere.

  • Real-time messaging
  • Multiplayer games
  • Live notifications
  • Stock market updates
  • Collaborative editors like Google Docs
  • Live dashboards
  • Online coding interviews

Basically, if users expect something to update instantly, WebSockets are probably involved.

What I Learned

1. Events are everything

Forget thinking in terms of requests.

With WebSockets, everything is an event.

  • Someone connects.
  • Someone sends a message.
  • Someone disconnects.

Once that clicked, building real-time apps suddenly felt much more natural.

2. One connection beats a thousand requests

Instead of sending hundreds of HTTP requests just to check for updates...

Keep one connection open.

Less network overhead.

Faster communication.

Happier servers.

Happier users.

Probably happier you.

3. The hard part isn't WebSockets

Getting two people to chat?

Easy.

Getting 20,000 people connected across multiple servers?

That's where things get interesting.

You'll eventually meet Redis, message brokers, sticky sessions, and other things that make system design interviews so... memorable.

A Tiny Example

io.on("connection", (socket) => {
  console.log("User connected");
 
  socket.on("message", (msg) => {
    io.emit("message", msg);
  });
 
  socket.on("disconnect", () => {
    console.log("User left");
  });
});

Not a lot of code.

A lot of magic.

The Takeaway

WebSockets completely change how you think about backend communication.

Instead of repeatedly asking the server if something happened...

The server simply tells you when it does.

No polling.

No refreshing.

No pretending a loading spinner is a real-time experience.

Once you build your first chat app with WebSockets, it's surprisingly hard to go back.

// HTTP:
// "Anything new?"
// "No."
// "Anything now?"
// "Still no."
 
// WebSocket:
// "I'll let you know."

Sometimes the fastest request...

...is the one you never have to make.