Back to Blog

Building Real-time Features with WebSockets

November 02, 20253 min read
WebSocketsReal-timeNode.jsArchitecture
Building Real-time Features with WebSockets

Building Real-time Features with WebSockets

HTTP is a request-response protocol. The client asks, the server answers, and the connection closes. But some features need the server to push data to the client without being asked: chat messages, live notifications, collaborative editing, real-time dashboards. That is where WebSockets come in.

When to Use WebSockets

Not every "live" feature needs WebSockets. A dashboard that updates every 30 seconds works fine with polling. A notification badge can use Server-Sent Events. WebSockets are the right choice when you need bidirectional, low-latency communication with frequent updates.

Good candidates: chat applications, multiplayer games, collaborative editing, live trading data, real-time analytics dashboards. Bad candidates: email notifications, periodic status updates, file uploads.

The Architecture

A WebSocket connection starts as an HTTP request that gets upgraded to a persistent TCP connection. Both the client and server can send messages at any time. The connection stays open until one side closes it.

In my projects, I use Socket.IO on top of raw WebSockets because it handles reconnection, room management, and fallback to polling automatically. The server architecture looks like this:

  1. Connection handler: Authenticates the user and assigns them to rooms.
  2. Event handlers: Process incoming messages and broadcast updates.
  3. Middleware: Validates payloads and checks permissions.
  4. Adapter: Redis adapter for horizontal scaling across multiple server instances.

Lessons from Building Chat

Building a chat feature taught me several things:

Message ordering is harder than you think. Network latency means messages can arrive out of order. Each message needs a timestamp from the server, not the client. The client sorts by server timestamp for display.

Presence is expensive. Showing who is online requires tracking connection state and broadcasting updates. With 1,000 users in a room, a single connect/disconnect event generates 1,000 messages. Batch presence updates and throttle broadcasts.

Offline handling matters. Users lose connection constantly. Queue messages locally when disconnected, sync when reconnected, and resolve conflicts with server-side timestamps.

Scaling WebSockets

A single WebSocket server handles maybe 10,000 concurrent connections. Beyond that, you need multiple server instances. The challenge is that a message sent to Server A needs to reach a user connected to Server B.

The solution is a pub/sub layer. Redis Pub/Sub or a message queue sits between the servers. When Server A receives a message for a room, it publishes to Redis. Server B subscribes to that room's channel and forwards the message to its connected clients.

Common Pitfalls

  1. Memory leaks from unclosed connections. Always clean up event listeners and connection references when a client disconnects.
  2. No authentication on connect. Validate the token during the WebSocket handshake, not after. An unauthenticated connection should never be established.
  3. Sending too much data. Send only the changed fields, not the entire object. A collaborative document should broadcast operations, not the full document on every keystroke.
  4. Ignoring backpressure. If a client cannot process messages as fast as they arrive, buffer or drop messages. Do not let a slow client cause memory issues on the server.

The Takeaway

WebSockets unlock features that feel magical to users. Real-time collaboration, instant messaging, and live data create experiences that polling simply cannot match. But they add complexity in scaling, error handling, and state management. Use them when the user experience demands it, and keep the architecture simple until the scale requires otherwise.

Enjoyed this post?

I'm always happy to connect and discuss ideas.