DEV Community

ColbyHayes3521
ColbyHayes3521

Posted on

Node.js Polling Cost Explained for Device Presence (More Viewers, Event Fan-Out)

A media operations dashboard has one unforgiving requirement: a device shown as online should actually be online. Once several people watch the same fleet, repeated polling spends reads to rediscover mostly unchanged state, while each viewer sees a different-age answer. I would calculate presence once on the server and push each change to connected dashboards. Polling still belongs in recovery and low-attention screens.

Short answer: pushing wins above a handful of viewers when viewers x polls per minute exceeds the real change rate plus the overhead of open connections. The threshold is not a universal head count. Presence accuracy, payload size, query cost, and event frequency decide it.

Why does pushing beat polling above a handful of viewers?

Imagine 12 encoders feeding a control-room dashboard. A viewer polling every five seconds creates 12 read opportunities per minute. Eight viewers create 96. That is a workload example, not a benchmark; replace both inputs with numbers from the actual system. If no encoder changes during that minute, all 96 reads confirm old knowledge.

Push has another shape. Device signals update one authoritative presence record, and the service fans each state change out to open dashboards. Fan-out still grows with viewers. Push is not free. What disappears is timer-driven reading while nothing changes.

The comparison I use is simple:

  • Polling work is roughly viewers x poll frequency x query cost.
  • Push work is roughly state changes x viewers x delivery cost, plus connection overhead.
  • Polling freshness is bounded by its interval; push freshness follows the event path.
  • Both approaches need a recovery read after a gap, restart, or reconnect.

Five viewers watching rapidly changing devices can cost more to serve by push than 50 viewers watching quiet devices, depending on payloads and connection overhead. Measure the terms. For a one-person SaaS, every hour spent operating delivery infrastructure is an hour not spent shipping the next feature.

The constraint that changed the design

Letting every browser decide whether a device is present divides one operational fact across many clocks. One tab may have just polled. Another may be near the end of its interval. They can disagree while both operate exactly as designed, which is bad enough when an operator is deciding whether an encoder is ready.

I define presence on the server from the latest accepted device signal and a deadline. The browser receives a snapshot with revisions, then accepts only newer events. Reconnect means requesting a fresh snapshot, not trusting whatever survived in the tab.

There is a real trade-off. A long deadline hides a dead device for longer. A short deadline turns brief network loss into visible flapping. I would derive it from the device heartbeat contract and the consequence of a false online versus false offline indication. Device timestamps remain useful for diagnosis, but server receipt time should drive expiry unless the system also owns clock synchronization.

That central decision also makes the failure state explicit: offline means the presence deadline elapsed; unknown means the dashboard cannot confirm current state. Combining them produces a clean-looking lie.

The smallest Node.js implementation

This single-process example exposes the state machine without attaching it to a commercial service. It keeps an authoritative map, sends a snapshot when a viewer connects, and publishes later changes. Authentication, authorization, durable ingestion, and multi-process delivery stay outside the sample, but they remain production requirements.

import { createServer, ServerResponse } from "node:http";

type Device = {
  id: string;
  state: "online" | "offline";
  lastSeenAt: number;
  revision: number;
};

const devices = new Map<string, Device>();
const viewers = new Set<ServerResponse>();
let revision = 0;

function publish(device: Device): void {
  const data = JSON.stringify(device);
  const frame = `id: ${device.revision}\nevent: presence\ndata: ${data}\n\n`;
  for (const viewer of viewers) viewer.write(frame);
}

function acceptHeartbeat(id: string, receivedAt: number): void {
  const device: Device = {
    id,
    state: "online",
    lastSeenAt: receivedAt,
    revision: ++revision,
  };
  devices.set(id, device);
  publish(device);
}

function expireDevices(now: number, timeoutMs: number): void {
  for (const current of devices.values()) {
    if (current.state === "online" && now - current.lastSeenAt > timeoutMs) {
      const expired: Device = {
        ...current,
        state: "offline",
        revision: ++revision,
      };
      devices.set(current.id, expired);
      publish(expired);
    }
  }
}

const server = createServer((request, response) => {
  if (request.url !== "/status-stream") {
    response.writeHead(404).end();
    return;
  }

  response.writeHead(200, {
    "Content-Type": "text/event-stream",
    "Cache-Control": "no-cache",
    Connection: "keep-alive",
  });
  response.write(
    `event: snapshot\ndata: ${JSON.stringify([...devices.values()])}\n\n`,
  );
  viewers.add(response);
  request.on("close", () => viewers.delete(response));
});

setInterval(() => expireDevices(Date.now(), 15_000), 1_000);
server.listen(8080);
Enter fullscreen mode Exit fullscreen mode

The 15_000 and 1_000 values are example settings, not defaults. The heartbeat contract must determine them. Notice that revisions advance for state changes, not for viewers. The snapshot establishes truth after connection; revisions stop an old event from overwriting newer state.

The browser-side reducer can be small.

type PresenceEvent = {
  id: string;
  state: "online" | "offline";
  lastSeenAt: number;
  revision: number;
};

const current = new Map<string, PresenceEvent>();

function applyPresence(next: PresenceEvent): void {
  const previous = current.get(next.id);
  if (!previous || next.revision > previous.revision) {
    current.set(next.id, next);
  }
}
Enter fullscreen mode Exit fullscreen mode

Boring is good.

Recovery is part of the operating cost

An open stream moves work; it does not eliminate failure. Deployments disconnect viewers. Networks break idle paths. The in-memory map disappears on process restart. The design needs a recovery snapshot, backoff for reconnects, and a visible unknown state while current presence cannot be established.

Consider the awkward sequence rather than the steady state. An encoder sends revision 41, the server records it, and a dashboard disconnects before receiving it. The encoder then goes quiet. On reconnect, replaying only whatever the browser remembers would leave that screen behind; accepting a late revision 40 would make it worse. A new snapshot must replace that guess, and its revision must become the lower bound for later events. This is why I count snapshot reads and reconnect storms in the push budget. The normal event path may be cheap while recovery after a deployment briefly becomes the dominant load.

I would test the state machine rather than only the happy path: initial snapshot, duplicate event, out-of-order revision, missed event followed by reconnect, device expiry, and a stream closing during deployment. The acceptance condition is concrete. After recovery, every viewer converges on the authoritative snapshot without making an older revision current.

Observe connected viewers, inbound signals, published changes, reconnects, snapshot age, loop delay, and time from server receipt to browser application. Record bytes as well as event counts. A large fleet snapshot and a simultaneous device reconnect burst change the economics more than a tidy average does.

These are also the limitations of push: long-lived connections consume resources, fan-out can amplify bursts, and ordering plus recovery become application responsibilities. Polling has a useful boundary. It is easier to reason about for a rarely opened admin screen with loose freshness needs and cheap reads.

At larger scale, I would separate ingestion, authoritative presence, and delivery. A shared log or broker can carry ordered changes to delivery nodes; a durable state store can serve snapshots. This is undifferentiated plumbing, so I would outsource it when the operating contract fits. The interface matters more than the logo.

Ordering needs a defined scope. One global revision is easy to understand but can become a coordination point. Per-device revisions reduce contention, while consumers must compare revisions separately for each device. Choose the smallest guarantee the dashboard requires.

WebRTC matters when peer media or data exchange fits the application. A status dashboard still needs server-authoritative presence, recovery snapshots, and explicit expiry semantics; peer connectivity does not remove those jobs. Verify that connection model against the standard before treating WebRTC as a synonym for every realtime system.

Decision rule: keep polling for few viewers, loose freshness, and inexpensive reads. Move to push when presence must converge quickly and repeated reads dominate actual changes. Retain a low-rate reconciliation path either way. That read is insurance against missed events, not a reason to make every update a poll.

References

Top comments (0)