Skip to content

WebSocket — alert when the disconnect rate spikes

Real-time chat is up, but disconnects/sec are 3× the baseline — some intermediate nginx started dropping connections. Want an endpoint with the live rate.

Recipe

js
// Track 60-second sliding window of disconnects
const WebSocket = require('ws');
const buckets = new Array(60).fill(0);
let cursor = 0;

setInterval(() => {
  cursor = (cursor + 1) % 60;
  buckets[cursor] = 0;
}, 1000);

const wss = new WebSocket.Server({ port: 8081 });
wss.on('connection', (ws) => {
  ws.on('close', () => { buckets[cursor]++; });
});

require('http').createServer((_req, res) => {
  const total = buckets.reduce((a, b) => a + b, 0);
  const threshold = parseInt(process.env.WS_DISC_THRESHOLD || '30', 10);
  res.end(total >= threshold ? `high ${total}` : `ok ${total}`);
}).listen(8082);

Same thing in Enterno.io

An Enterno HTTP monitor on :8082 with keyword "ok" alerts the moment the rate crosses threshold. Pro+ stores history so you can spot patterns (e.g. spikes at 14:00 daily on auto-restart).

Set up HTTP monitor → ← All recipes

Related recipes