WebSocket heartbeat mechanism
There are two main ways for the front-end to implement the WebSocket heartbeat mechanism:
Use setInterval to send heartbeat packets on a scheduled basis. Recreate the WebSocket connection when the front-end listens to the onclose() event of WebSocket.
The first method can cause significant pressure on the server, as even if the WebSocket connection is normal, it still needs to send heartbeat packets on a scheduled basis, which consumes server resources. The second method, although reducing the burden on the server, may result in some data loss during reconnection.
WebSocket heartbeat packet mechanism
The WebSocket heartbeat packet is the liveliness mechanism of the WebSocket protocol, used to maintain long connections. An effective heartbeat packet can prevent WebSocket from automatically disconnecting when not communicating for a long time.
Heartbeat packets refer to empty data packets sent by WebSocket within a certain time interval. The common WebSocket heartbeat packet mechanism is as follows:
The client sends heartbeat packets to the server on a scheduled basis to maintain a long connection. The server sends heartbeat packets to the client on a scheduled basis to check if the client connection is normal. Send heartbeat packets in both directions.
The principle of WebSocket heartbeat mechanism
The principle of WebSocket heartbeat mechanism is to use heartbeat packets to send and receive data in a timely manner, ensuring that WebSocket long connections are not disconnected. The principle of WebSocket heartbeat mechanism can be explained by the following process:
The client establishes a WebSocket connection. The client sends a heartbeat packet to the server, and the server receives and returns a response indicating receipt of the heartbeat packet. When the server does not receive the heartbeat data packet sent by the client in a timely manner, the server will send a request to close the connection. The server sends heartbeat packets to the client on a scheduled basis, and the client receives and returns a response indicating receipt of the heartbeat packet. When the client does not receive the heartbeat data packet sent by the server in a timely manner, the client will reconnect to WebSocket.
The necessity of WebSocket heartbeat mechanism
The WebSocket heartbeat mechanism is necessary as it can keep WebSocket connections connected for a long time and prevent disconnection from occurring. At the same time, the heartbeat mechanism can also check the status of WebSocket connections and promptly handle abnormal situations.
The role of WebSocket heartbeat mechanism
- Keep the WebSocket connection from being disconnected.
- Detect the status of WebSocket connections and promptly handle any abnormal situations.
- Reduce the consumption of WebSocket connections and server resources.
WebSocket reconnection mechanism
When WebSocket sends and receives data, it may disconnect due to network issues, server downtime, and other factors. In this case, WebSocket reconnection mechanism needs to be used to reconnect.
The WebSocket reconnection mechanism can be implemented in the following ways:
The front-end listens to the onclose() event of WebSocket and creates a new WebSocket connection. Use WebSocket plugins or libraries, such as Sockjs, Stompjs, etc. Use heartbeat mechanism to detect WebSocket connection status and automatically reconnect. Use disconnected reconnection plugins or libraries, such as ReconnectingWebSocket.
The Disadvantages and Shortcomings of WebSocket
WebSocket requires both the browser and server sides to support this protocol. WebSocket will increase the burden on the server and is not suitable for large-scale connection application scenarios.