nchan_websocket_ping_interval
The 'nchan_websocket_ping_interval' directive specifies the interval at which ping messages are sent to WebSocket connections to keep them alive.
Description
The 'nchan_websocket_ping_interval' directive is critical for maintaining persistent connections with WebSocket clients in the Nchan module of NGINX. This directive defines the frequency at which ping messages are dispatched over open WebSocket connections, effectively ensuring that the connection remains open and active. By sending periodic pings, the server can detect and clear idle connections, thus optimizing resource usage and improving performance.
The interval is specified in seconds, and setting an appropriate interval is essential to balance between maintaining active connections and avoiding unnecessary network traffic. If the interval is too long, connections may timeout due to inactivity; if too short, it could burden the server with excessive ping messages. It is especially useful in scenarios with a large number of clients where timeouts could occur between heartbeats due to network issues or if clients go offline. The directive can be utilized in various contexts such as server, location, or if within a location block, allowing for flexibility in configuration based on specific application needs.
Config Example
location /ws {
nchan_websocket_ping_interval 30;
nchan_pubsub;
}Be cautious not to set the interval too low to avoid overwhelming clients with pings.
If the server is behind a firewall or proxy, ensure that it allows WebSocket ping messages to pass through safely.