nchan_websocket_client_heartbeat
The nchan_websocket_client_heartbeat directive defines the interval for sending heartbeat messages through a WebSocket connection to maintain an active session.
Description
The nchan_websocket_client_heartbeat directive is used in the NGINX Nchan module to specify the interval, in seconds, at which heartbeat messages are sent over an active WebSocket connection. This is particularly important for ensuring that idle WebSocket connections remain open and are not mistakenly closed by intermediate proxies or firewalls due to inactivity.
When configured, NGINX will periodically send a message over the WebSocket channel to verify the connection status. This helps in maintaining a persistent connection by keeping the client engaged and is crucial for applications that require real-time data transmission. The directive accepts two arguments: the first specifies the interval for sending heartbeats, while the second specifies the data payload (if any) that should be sent in these heartbeat messages. The default heartbeat interval is not set, which means heartbeats will not be sent unless explicitly specified.
Depending on the application requirements, it is essential to configure this directive correctly to optimize WebSocket performance. Too lengthy intervals may allow connections to drop, while too frequent heartbeats could lead to unnecessary traffic and processing overhead on both servers and clients.
Config Example
location /ws {
nchan_websocket_client_heartbeat 30 "{"keep_alive": true}";
}Ensure that the heartbeat interval does not conflict with other timeouts, such as WebSocket ping-pong mechanisms.
Sending large payloads in heartbeat messages can lead to increased bandwidth usage and potential issues with network latency.