WebSockets & Real-Time Communication

What's the purpose of the WebSocket heartbeat mechanism?
function setupHeartbeat(ws) {
    const pingInterval = setInterval(() => {
        if (ws.readyState === WebSocket.OPEN) {
            ws.send('ping');
        }
    }, 30000);

    ws.onmessage = (event) => {
        if (event.data === 'pong') {
            console.log('Connection alive');
        }
    };

    ws.onclose = () => clearInterval(pingInterval);
}
Next Question (4/17)