midWebSockets
What is heartbeat mechanism in WebSockets?
Updated May 6, 2026
Short answer
Heartbeat is a periodic ping-pong mechanism to check if connection is alive.
Deep explanation
Servers and clients send ping/pong messages at intervals to detect broken or idle connections. If no response is received, connection is closed.
Real-world example
Live trading systems detecting inactive clients.
Common mistakes
- Not implementing heartbeat leading to zombie connections.
Follow-up questions
- What is pong response?
- Why is heartbeat needed?