DEV Community

ExtensionBooster
ExtensionBooster

Posted on

How to Build a Real-Time Feature Without WebSockets Complexity

WebSockets are powerful but come with operational complexity. Here are simpler alternatives that work for most use cases.

Your Actual Options

1. Server-Sent Events (SSE)

One-way server-to-client streaming. Incredibly simple:

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

  const interval = setInterval(() => {
    res.write('data: { "time": "' + Date.now() + '" }

');
  }, 1000);

  req.on('close', () => clearInterval(interval));
});
Enter fullscreen mode Exit fullscreen mode

Works in every browser, falls back gracefully, handles reconnection automatically.

2. Polling with Exponential Backoff

For low-frequency updates:

async function poll(url, onUpdate) {
  let delay = 1000;
  while (true) {
    try {
      const data = await fetch(url).then(r => r.json());
      onUpdate(data);
      delay = 1000;
    } catch (e) {
      delay = Math.min(delay * 2, 30000);
    }
    await new Promise(r => setTimeout(r, delay));
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Firebase Realtime Database / Supabase Realtime

Managed real-time infrastructure. More moving parts but handles scale.

When to Use Each

Use Case Best Choice
Notifications, feeds SSE
Chat (low volume) SSE or Polling
Chat (high volume) WebSockets
Live sports scores SSE or WebSockets
Collaborative editing WebSockets

The Unpopular Truth

You probably don't need WebSockets. SSE handles 95% of "real-time" use cases with a fraction of the complexity. For extension backends needing real-time features, ExtensionBooster's API tools support SSE out of the box.

Top comments (0)