Heartbeat Monitoring
How It Works
The SDK monitors connection health using MQTT's built-in PING/PONG mechanism:
- The mqtt.js client sends a PINGREQ packet at the configured
keepAliveSecondsinterval - The SDK starts a timeout timer (configurable via
heartbeatTimeout, default 10 seconds) - If a PINGRESP is received before the timer expires → connection is healthy
- If the timer expires without a response → connection is stale
Stale Connection Recovery
When a stale connection is detected, the SDK:
- Force-disconnects the current session
- Waits 1 second
- Reconnects to the broker
- Restores all tracked subscriptions
Configuration
const client = new RealtimeClient({
endpoint: { ... },
heartbeatTimeout: 10000, // ms to wait for PINGRESP (default 10000)
});
Monitoring Heartbeat Health
Callback API
client.onHeartbeatChange((alive) => {
if (alive) {
console.log("Connection healthy");
} else {
console.log("Connection degraded");
}
});
RxJS API
client.heartbeat$.subscribe((alive) => {
updateStatusIndicator(alive);
});
Instant Check
const healthy = client.isHeartbeatHealthy();
React
import { useLinkStatus } from "@armaanjain/courier-web-sdk/react";
function StatusIndicator() {
const { isLive, heartbeatOk } = useLinkStatus();
return <span>{isLive && heartbeatOk ? "Online" : "Degraded"}</span>;
}