What is a WebSocket ping/pong?
Answer
WebSocket ping/pong is a built-in heartbeat mechanism defined in the WebSocket protocol. The server sends a ping frame (opcode 0x9), and the client is required by the spec to respond with a pong frame (opcode 0xA) containing the same payload. This serves two purposes: (1) Connection keepalive — prevents firewalls, proxies, and load balancers from closing idle connections due to timeout; (2) Liveness detection — if the server sends a ping and doesn't receive a pong within a timeout, it knows the client is disconnected and can clean up server-side resources. The browser's WebSocket API handles pong responses automatically — you don't need to implement it manually. The ws Node.js library fires a ping event when a ping is received and a pong event when a pong arrives.
Previous
How do you send data over a WebSocket connection?
Next
What happens when a WebSocket connection is lost?