Connection Establishment
WebSocket connections are established via standard WebSocket handshake:- The client closes the connection
- The server detects inactivity (heartbeat timeout)
- The maximum session duration is reached
- An error occurs
Heartbeat Mechanism
Lasso implements WebSocket ping/pong heartbeats to detect dead connections and ensure connection health.Heartbeat Configuration
| Parameter | Value | Description | |-----------|-------|-------------|| | Heartbeat Interval | 30 seconds | Server sends ping every 30 seconds | | Pong Timeout | 5 seconds | Client must respond with pong within 5 seconds | | Max Missed Heartbeats | 2 | Connection closed after 2 missed pongs |Heartbeat Flow
-
Server sends ping (every 30 seconds)
-
Client responds with pong (within 5 seconds)
-
Timeout detection
- If pong not received within 5 seconds, missed heartbeat counter increments
- After 2 missed heartbeats (10 seconds total), connection is closed
-
Connection closure
Heartbeat Timeline
Client-Initiated Pings
Clients can also send ping frames to the server. Lasso responds with pong:Session Duration
WebSocket connections have a maximum session duration:| Parameter | Value |
|---|---|
| Maximum Session Duration | 2 hours (7,200,000 ms) |
Session Timeout Behavior
After 2 hours, the server closes the connection:Timeout Summary
| Timeout Type | Duration | Behavior |
|---|---|---|
| Heartbeat interval | 30s | Server sends ping |
| Pong timeout | 5s | Wait for pong response |
| Missed heartbeat tolerance | 2 missed pongs | Close connection |
| Maximum session | 2 hours | Close connection |
| Total heartbeat failure time | ~10s | Time from first missed pong to closure |
Connection Closure
Server-Initiated Closure
The server closes connections in these scenarios: | Reason | WebSocket Code | Message | |--------|----------------|---------|| | Heartbeat timeout | 1002 (Protocol Error) | “Heartbeat timeout - no pong responses” | | Session timeout | 1000 (Normal Closure) | “Session timeout” | | Rate limit exceeded | 1008 (Policy Violation) | “Rate limit exceeded” | | Internal error | 1011 (Internal Error) | Error details |Client-Initiated Closure
Clients can close connections at any time:Reconnection Strategy
Clients should implement reconnection logic to handle disconnections:Subscription Cleanup
When a connection closes, Lasso automatically:- Unsubscribes all active subscriptions for that connection
- Releases upstream provider subscriptions
- Cleans up internal subscription tracking
Keepalive Best Practices
For Clients
-
Respond to pings promptly
- Most WebSocket libraries handle ping/pong automatically
- Ensure your client doesn’t block pong responses
-
Monitor connection health
-
Handle session timeouts gracefully
For Long-Running Connections
-
Plan for session expiration
- Connections close after 2 hours
- Implement automatic reconnection
- Re-establish subscriptions on reconnect
-
Monitor heartbeat status
-
Implement exponential backoff
- Use exponential backoff for reconnection attempts
- Add jitter to prevent thundering herd
- Cap maximum delay (e.g., 30 seconds)
Connection Monitoring
Monitor connection health using these indicators:Client-Side Monitoring
Server-Side Telemetry
Lasso emits telemetry events for connection lifecycle:[:lasso, :websocket, :connected]- Connection established[:lasso, :websocket, :disconnected]- Connection closed[:lasso, :websocket, :heartbeat, :timeout]- Heartbeat timeout[:lasso, :websocket, :session, :timeout]- Session timeout
Error Scenarios
Network Interruption
If network connectivity is lost:- Client’s TCP connection breaks
- Client receives connection close event
- Client should reconnect with exponential backoff
Firewall/Proxy Timeout
Some firewalls/proxies close idle connections:- Lasso’s 30-second heartbeat prevents most timeouts
- If your proxy has a shorter timeout, connections may close unexpectedly
- Implement reconnection logic to handle this
Server Restart
During server maintenance:- Server sends close frame (code 1001: Going Away)
- All connections close gracefully
- Clients should reconnect after a brief delay
WebSocket Frame Reference
Ping Frame
Pong Frame
Close Frame
1000- Normal closure1001- Going away (server shutdown)1002- Protocol error1008- Policy violation (rate limit)1011- Internal server error