WebSockets ping/pong, why not TCP keepalive?
The problems with TCP keepalive are:
- It is off by default.
- It operates at two-hour intervals by default, instead of on-demand as the Ping/Pong protocol provides.
- It operates between proxies rather than end to end.
- As pointed out by @DavidSchwartz, it operates between TCP stacks, not between the applications so therefore it doesn't tell us whether the application is alive or not.
The comparison with WebSockets ping/pong isn't meaningful. TCP keepalive is automatic, and timed, when enabled, whereas WebSocket ping/pong is executed as required by the application.
Besides the answer of EJP I think it might be also related to HTTP proxy mechanisms. Websocket connections can also run through a (HTTP) proxy server. In such cases the TCP keepalive would only check the connection up to the proxy and not the end-to-end connection.