Urgent.News

What's breaking now, across thousands of outlets.

Tech

Browsers answer WebSocket pings and never tell your JavaScript, so we send two heartbeats

A WebSocket that has died does not tell you it has died. Both ends go on believing in a connection that no longer exists: the server keeps writing into a socket nobody is reading, and the browser keeps showing whatever arrived last. For a dashboard that is a stale number. For a quiz, it is a player staring at question four while the room has moved to question six, and no error anywhere. Our app…

Both ends of a WebSocket connection continue to believe in a connection that no longer exists when the server writes into a socket nobody is reading, and the browser shows the last data received. This leads to stale numbers on dashboards and players stuck on outdated questions. The app runs in pubs, which have poor connectivity with captive portals, outdated routers, and spotty 4G.

Phones lock, freeze tabs, and come back hours later, causing open sockets to appear dead. The WebSocket protocol includes ping and pong control frames to detect liveness, but browsers hide these from JavaScript. The server pings and counts missed pongs, terminating the socket after two missed pongs. Heartbeat messages with no content are used to make noise the client can observe.

The watchdog checks for silence and reconnects if there's no message from the server within fifty seconds. Exponential back-off is used for reconnection, but a flat thirty-second delay is used when the client has already fallen back to polling.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Virtual PLC Lab: Ladder Logic Control of a Filling Tank with Factory I/O and OpenPLC over Modbus TCP

I wanted to learn PLC programming, but there was one obvious problem: I don't own a PLC, a tank, valves, or a panel with buttons. Buying that hardware just to practice ladder logic isn't realistic.

  • Author creates virtual PLC lab with 3D process simulator and soft PLC
  • Modbus TCP enables communication between simulator and PLC
  • Ladder logic controls tank operations based on button presses and time intervals

More from Friday 9 October →