Skip to main content
Lasso WebSocket connections include robust lifecycle management with heartbeat monitoring, automatic timeout handling, and configurable session limits.

Connection Establishment

WebSocket connections are established via standard WebSocket handshake:
Once connected, the connection remains open for bidirectional communication until:
  • 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

  1. Server sends ping (every 30 seconds)
  2. Client responds with pong (within 5 seconds)
  3. Timeout detection
    • If pong not received within 5 seconds, missed heartbeat counter increments
    • After 2 missed heartbeats (10 seconds total), connection is closed
  4. Connection closure

Heartbeat Timeline

Client-Initiated Pings

Clients can also send ping frames to the server. Lasso responds with pong:
Client pings don’t reset the server’s heartbeat timer, but they do confirm bidirectional connectivity.

Session Duration

WebSocket connections have a maximum session duration:

Session Timeout Behavior

After 2 hours, the server closes the connection:
Clients should reconnect and re-establish subscriptions after session timeout.

Timeout Summary

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:
When the connection closes, all active subscriptions are automatically cleaned up.

Reconnection Strategy

Clients should implement reconnection logic to handle disconnections:

Subscription Cleanup

When a connection closes, Lasso automatically:
  1. Unsubscribes all active subscriptions for that connection
  2. Releases upstream provider subscriptions
  3. Cleans up internal subscription tracking
Clients don’t need to manually unsubscribe before closing connections, but explicit unsubscription is cleaner:

Keepalive Best Practices

For Clients

  1. Respond to pings promptly
    • Most WebSocket libraries handle ping/pong automatically
    • Ensure your client doesn’t block pong responses
  2. Monitor connection health
  3. Handle session timeouts gracefully

For Long-Running Connections

  1. Plan for session expiration
    • Connections close after 2 hours
    • Implement automatic reconnection
    • Re-establish subscriptions on reconnect
  2. Monitor heartbeat status
  3. 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:
  1. Client’s TCP connection breaks
  2. Client receives connection close event
  3. 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:
  1. Server sends close frame (code 1001: Going Away)
  2. All connections close gracefully
  3. Clients should reconnect after a brief delay

WebSocket Frame Reference

Ping Frame

Pong Frame

Close Frame

Common close codes:
  • 1000 - Normal closure
  • 1001 - Going away (server shutdown)
  • 1002 - Protocol error
  • 1008 - Policy violation (rate limit)
  • 1011 - Internal server error