WebSockets have the option of sending pings to the other end, where the other end should respond pong.
After receiving a Ping frame, the endpoint MUST send a Pong frame in response if it has not already received a Close frame. You should answer with the Pong frame as soon as it becomes practical.
TCP offers something similar in the form of keepalive:
[Y] you send your peer a keepalive control packet with no data in it and the ACK flag is turned on. You can do this because of the TCP / IP specifications, as a kind of duplicate ACK, and the remote endpoint will not have any arguments, because TCP is a flow-oriented protocol. On the other hand, you will receive a response from a remote host (which should not support keepalive at all, only TCP / IP), without data and an ACK.
I would think that TCP keepalive is more efficient because it can be processed in the kernel without the need to transfer data to user space, parse the websocket frame, process the response frame and pass it to the kernel for the gearbox. It also reduces network traffic.
In addition, WebSockets are explicitly specified to always work over TCP; they are not aggregated transport layers, so TCP keepalive is always supported:
The WebSocket protocol is an independent TCP protocol.
So why would you ever want to use ping / pong WebSocket instead of TCP keepalive?
websocket tcp keep-alive
Thomas Apr 23 '14 at 8:00 2014-04-23 08:00
source share