Overview
WebSockets enable bidirectional, real-time communication between clients and servers. TrustGate acts as a proxy for WebSocket connections, providing features such as:- Connection management and load balancing
- Health checks for upstream services
- Plugin execution for WebSocket messages
- Two communication modes: direct and multiplexed
Configuration
To enable WebSocket support for an upstream service, you need to include awebsocket_config
section in your upstream configuration.
Example Configuration
WebSocket Configuration Options
Option | Type | Description | Default |
---|---|---|---|
enable_direct_communication | boolean | When true, each client connection gets its own dedicated connection to the upstream service. When false, connections are multiplexed. | false |
return_error_details | boolean | When true, detailed error messages are returned to clients. | false |
ping_period | string | The interval for sending ping messages to keep connections alive (e.g., ”30s”). | ”30s” |
pong_wait | string | The timeout for receiving pong responses (e.g., “2m”). | ”45s” |
handshake_timeout | string | The timeout for the initial WebSocket handshake (e.g., “5s”). | “5s” |
read_buffer_size | integer | The size of the read buffer in bytes. | 4096 |
write_buffer_size | integer | The size of the write buffer in bytes. | 4096 |
Health Check Configuration
Health checks help ensure that only healthy upstream services receive WebSocket connections.Option | Type | Description | Default |
---|---|---|---|
passive | boolean | When true, enables passive health checks that monitor actual traffic. | false |
path | string | The path to use for active health checks. | "" |
headers | object | Headers to include in health check requests. | {} |
threshold | integer | Number of failures before marking a target as unhealthy. | 3 |
interval | integer | Time in seconds before resetting the failure count. | 60 |
Communication Modes
TrustGate supports two communication modes for WebSockets:Direct Communication Mode
In direct communication mode (enable_direct_communication: true
), each client connection gets its own dedicated connection to the upstream service. This mode is useful when:
- You need to preserve connection-specific state on the upstream service
- You want to ensure a 1:1 mapping between client and upstream connections
- You need to minimize latency
Multiplexed Mode
In multiplexed mode (enable_direct_communication: false
), TrustGate maintains a pool of connections to upstream services and routes messages from multiple clients through these shared connections. This mode is useful when:
- You want to reduce the number of connections to your upstream services
- Your upstream services don’t need to maintain connection-specific state
- You want to optimize resource usage
Path Configuration
WebSocket endpoints should be configured with paths that include/ws/
to be properly identified by TrustGate. For example:
Plugin Support
TrustGate’s plugin system works with WebSocket connections, allowing you to:- Execute plugins at the PreRequest stage (before forwarding messages to upstream)
- Execute plugins at the PostResponse stage (before returning messages to clients)
- Modify message content
- Implement custom authentication or authorization
- Add logging or metrics collection
Error Handling
Whenreturn_error_details
is enabled, TrustGate will send detailed error messages to clients when issues occur, such as:
- Connection failures to upstream services
- Plugin execution errors
- Timeout errors
Best Practices
- Set appropriate timeouts: Configure
ping_period
,pong_wait
, andhandshake_timeout
based on your application’s needs. - Enable health checks: Use health checks to ensure traffic is only sent to healthy upstream services.
- Choose the right communication mode: Use direct communication when you need 1:1 connection mapping, and multiplexed mode when optimizing for resource usage.
- Monitor connection counts: Set appropriate
max_connections
in your global WebSocket configuration to prevent resource exhaustion. - Implement proper error handling: Client applications should handle reconnection logic when connections are closed.