Node.js Presence: Polling vs. Push Costs

Zusammenfassung

Pixel shares a practical guide to the costs of Node.js polling versus pushing device presence updates to dashboards. It compares viewer polling frequency and query costs with event fan-out, delivery costs, and connection overhead, while explaining when polling remains useful for recovery and low-attention screens.

DEV Community

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();
const viewers = new Set();
let revision = 0;

function publish(device: Device): void {
const data = JSON.stringify(device);
const frame = `id: ${device.revision}...
Node.js Presence: Polling vs. Push Costs
Powered by Readybot.io

Das Neueste von Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …