🚀 Introduction to Real-Time Web

Saravana RajaSaravana Raja
6 min read

For those of us after years into this dev life:

  • 💻 "The weekend is for upskilling!" (then promptly opens Netflix)

  • ☕ Filter coffee - the actual backbone of my productivity

  • 🌙 Midnight message: “Available for Quick Call?”

  • 👥 Catching up with friends who switched jobs… because of career inspiration

  • 📚 A growing list of “absolutely starting soon” courses (any day now)

  • 💪 Consistent gym attendance (hey, one routine we stick to)

Building real-time applications is supposed to add excitement to the dev life—but let's be real, some parts are more like glorified waiting rooms. Here’s the rundown on the methods available, the reality, and a sprinkle of sarcasm.


Real-Time Techs: The Honestly Real Rundown

1. Short Polling

The "refresh-until-something-happens" approach.

Short polling is like constantly checking to see if you've made progress on your "to-learn" list… every weekend. It’s simple but resource-hungry. The client keeps asking, “Anything new?” until the server finally has something to say.

// Client
class StatusChecker {
  constructor(url, interval = 5000) {
    this.url = url;
    this.interval = interval;
  }

  start() {
    setInterval(async () => {
      try {
        const response = await fetch(this.url);
        const data = await response.json();
        this.handleUpdate(data);
      } catch (error) {
        console.error("Update failed:", error);
      }
    }, this.interval);
  }
}

// Server (Express)
app.get("/status", (req, res) => {
  res.json({
    timestamp: new Date(),
    data: getCurrentData(),
  });
});

Pros: 🟢 Quick to set up

Cons: 🔴 Wastes resources. Every request is a bit like "refreshing Netflix recommendations."

2. Long Polling

"I'll just wait here until motivation hits" (and it might be a while).

In long polling, the client sends a request and waits… and waits... until the server finally sends an update. It's like sitting down to work on that online course, only to sit there wondering what else you could be doing.

// Client
class LongPoller {
  async startPolling() {
    try {
      const response = await fetch('/updates');
      const data = await response.json();
      this.handleData(data);
      this.startPolling();
    } catch {
      setTimeout(() => this.startPolling(), 3000);
    }
  }
}

// Server
app.get('/updates', async (req, res) => {
  try {
    const update = await waitForNewData();
    res.json(update);
  } catch (error) {
    res.status(500).send('Error occurred');
  }
});

Pros: 🟢 Fewer requests than short polling

Cons: 🔴 Holds connections open, so scaling gets tricky

3. Server-Sent Events (SSE)

The friend who only talks about themselves.

SSE is perfect for one-way notifications where the server does all the talking and the client listens (whether they want to or not). Think of it like that friend who only likes to talk about their day and never asks about yours. It's ideal for notifications and one-way updates.

// Client
class EventSourceHandler {
  constructor(url) {
    this.eventSource = new EventSource(url);
    this.setup();
  }

  setup() {
    this.eventSource.onmessage = (event) => {
      const data = JSON.parse(event.data);
      this.handleUpdate(data);
    };

    this.eventSource.onerror = () => {
      this.eventSource.close();
      setTimeout(() => this.reconnect(), 3000);
    };
  }
}

// Server
app.get('/events', (req, res) => {
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Connection', 'keep-alive');

  const interval = setInterval(() => {
    res.write(`data: ${JSON.stringify({
      time: new Date(),
      data: getLatestData()
    })}\n\n`);
  }, 1000);

  req.on('close', () => clearInterval(interval));
});

Pros: 🟢 Real-time updates without repetitive requests

Cons: 🔴 One-way communication only, so the client doesn't get to "talk back."

4. WebSocket

The reliable, low-latency solution for night-owl coders.

WebSockets allow true, two-way communication, making it ideal for live applications like multiplayer games or chat. Think of it as having a direct line open at all times, even at 2 a.m., when inspiration strikes.

// Client
class WebSocketClient {
  constructor(url) {
    this.connect(url);
  }

  connect(url) {
    this.ws = new WebSocket(url);

    this.ws.onopen = () => {
      console.log('Connected');
      this.startHeartbeat();
    };

    this.ws.onmessage = (event) => {
      this.handleMessage(JSON.parse(event.data));
    };

    this.ws.onclose = () => {
      setTimeout(() => this.connect(url), 3000);
    };
  }

  send(data) {
    if (this.ws.readyState === WebSocket.OPEN) {
      this.ws.send(JSON.stringify(data));
    }
  }
}

// Server
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', (ws) => {
  ws.on('message', (message) => {
    wss.clients.forEach(client => {
      if (client !== ws && client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  });
});

Pros: 🟢 True real-time, bidirectional communication

Cons: 🔴 Requires consistent connection management (a.k.a. babysitting)

5. Socket.IO

For when you just want it to "work."

Socket.IO is like WebSockets but adds extra features, like automatic reconnections, room-based messaging, and even some fallbacks. It's WebSockets with fewer headaches.

// Client
const socket = io('http://localhost:3000', {
  reconnectionAttempts: 5,
  reconnectionDelay: 1000
});

socket.on('connect', () => {
  console.log('Connected');
  socket.emit('join', { room: 'main' });
});

socket.on('update', (data) => {
  handleUpdate(data);
});

// Server
const io = require('socket.io')(server);

io.on('connection', (socket) => {
  socket.on('join', (data) => {
    socket.join(data.room);
  });

  socket.on('message', (data) => {
    io.to(data.room).emit('update', {
      data: data,
      time: new Date()
    });
  });
});

Pros: 🟢 Takes care of reconnection, fallbacks, and supports rooms

Cons: 🔴 A bit more overhead and can lead to overengineering if you're not careful

Real-Time Technology Choice Guide

NeedChooseBecause
Simple updatesShort PollingMinimal effort
Less server requestsLong PollingReduces wasteful pings
One-way server notificationsSSEQuick and efficient
Bidirectional updatesWebSocketLow-latency communication
Full-featured solutionSocket.IOPacks everything you need

Best Practices

  1. Handle disconnections: Real-time apps drop out too.

    • Implement reconnection logic

    • Provide clear feedback to users

  2. Implement retries: Users will be grateful for the save.

    • Use exponential backoff for retries

    • Set a maximum retry limit

  3. Monitor performance: Real-time sounds cool, but it's server-intensive.

    • Keep an eye on server load

    • Implement rate limiting if necessary

  4. Keep security in mind: Exposing more data = higher risk.

    • Use secure WebSocket connections (wss://)

    • Implement proper authentication and authorization

  5. Validate data: Because no one likes unexpected surprises.

    • Sanitize incoming data on both client and server

    • Use schemas to ensure data integrity


Real-World Considerations

  1. Start Simple: Pick only what your app truly needs.

    • Don't overengineer your initial implementation

    • You can always scale up later

  2. Scale Carefully: Real-time's great until you have 10,000 users.

    • Consider using a message queue for high-load scenarios

    • Look into horizontal scaling options early

  3. Plan for Failures: Real-time is only as good as its recovery plan.

    • Implement proper error handling

    • Have a fallback mechanism (e.g., reverting to polling if WebSocket fails)

  4. Maintenance: Each real-time feature comes with its own upkeep.

    • Regular testing of real-time features

    • Keep libraries and dependencies up to date


Conclusion

Real-time technology can add sparkle to your app, but remember to choose the one that actually fits your needs. And, for those still working on that "upskilling" plan, simpler might just be better. So, here's to keeping it real-time, without over-complicating the weekend Netflix break!

And speaking of which… time for another filter coffee. ☕


Did this guide help you navigate the world of real-time web development? Share your thoughts and experiences in the comments below!

Don't forget to like and share if you found this useful. Happy coding! 🚀👨‍💻👩‍💻

0
Subscribe to my newsletter

Read articles from Saravana Raja directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saravana Raja
Saravana Raja