DEV Community

Vigilmon
Vigilmon

Posted on

How to Monitor WebSocket Connections and Real-Time Apps in 2025

Why Monitor WebSockets?

Real-time applications built on WebSockets introduce unique monitoring challenges. Unlike HTTP, WebSocket connections are persistent - a dropped connection can silently break chat, dashboards, or collaborative editors.

Key Metrics to Track

  • Connection establishment - did the handshake succeed?
  • Connection duration - are connections staying alive?
  • Message throughput - messages per second
  • Unexpected drops - reconnection rate

Tracking Connection State

`javascript
const activeConnections = new Map();

wss.on('connection', (ws, req) => {
const id = crypto.randomUUID();
const startTime = Date.now();
activeConnections.set(id, { startTime, messagesIn: 0, messagesOut: 0 });

ws.on('close', (code) => {
const conn = activeConnections.get(id);
console.log(JSON.stringify({
event: 'ws_closed',
duration_ms: Date.now() - conn.startTime,
close_code: code
}));
activeConnections.delete(id);
});
});
`

Measuring Latency

javascript
function pingLatency(ws) {
const t = Date.now();
ws.ping();
ws.once('pong', () => console.log('latency_ms:', Date.now() - t));
}
// Call every 30s per connection

External Uptime Monitoring

WebSocket servers expose an HTTP endpoint for the upgrade handshake. Point Vigilmon at that URL - it checks every minute and alerts you the moment the endpoint stops accepting connections.

`ash

Vigilmon monitors this HTTP endpoint

curl -i https://yourapp.com/ws

Should return 101 Switching Protocols

`

Configure alerts for:

  • Response time > 500 ms
  • Status != 101 / 200
  • Downtime > 1 minute

Metrics Endpoint

javascript
app.get('/metrics/ws', (_req, res) => {
res.json({
active_connections: activeConnections.size,
timestamp: new Date().toISOString()
});
});

Alert Thresholds

Condition Threshold Severity
Connections drop to 0 immediate Critical
Error rate > 5% 5 min High
Avg latency > 200 ms 10 min Medium

Takeaway

Monitor the upgrade endpoint externally, track per-connection metrics internally, and alert on reconnection spikes before a full outage develops. Tools like Vigilmon give you instant visibility without complex infrastructure.

Top comments (0)