Skip to main content

On Socket Event

This block is triggered when socket connection events occur. Use it to handle connection status changes and errors.

On Socket Event Block

Overview

The On Socket Event block detects socket connection lifecycle events, allowing your application to respond to connection changes and errors.

Event Types

This block triggers on various socket events:

Connected

  • Socket connection successfully established
  • Ready to send/receive data
  • TLS handshake completed (if applicable)

Disconnected

  • Connection closed normally
  • Remote server closed connection
  • Local disconnect initiated

Error

  • Connection failed
  • Timeout occurred
  • Network error
  • TLS/SSL error

Timeout

  • Connection attempt timeout
  • Read/write timeout
  • Keep-alive timeout

Configuration

  • Socket Connection: Which socket to monitor
  • Event Type: Which events to trigger on
  • Output: Variable to store event details

Event Information

Access event details:

  • Event type (connected/disconnected/error/timeout)
  • Error code and message
  • Timestamp
  • Connection state

Use Cases

Common event handling scenarios:

On Connected

  • Start sending data
  • Initialize protocol handshake
  • Log successful connection
  • Update connection status

On Disconnected

  • Clean up resources
  • Attempt reconnection
  • Queue pending messages
  • Log disconnection

On Error

  • Log error details
  • Implement retry logic
  • Switch to backup server
  • Alert monitoring system

On Timeout

  • Retry connection
  • Increase timeout value
  • Check network status
  • Log timeout event

Error Recovery

Implement robust error handling:

  1. Detect error event
  2. Log error details
  3. Wait before retry (exponential backoff)
  4. Attempt reconnection
  5. If fails repeatedly, alert operator

Connection Monitoring

Monitor socket health:

  • Track connection uptime
  • Count connection failures
  • Measure connection latency
  • Alert on frequent disconnects

State Management

Manage connection state:

  • Store current connection status
  • Update application state
  • Enable/disable features based on connectivity
  • Provide user feedback

Reconnection Logic

Implement automatic reconnection:

On Socket Event (Disconnected):
1. Wait 5 seconds
2. Socket Connect
3. If fails, wait 10 seconds
4. Try again with exponential backoff
5. Max retry limit

Multiple Connections

Monitor multiple sockets:

  • Create separate event blocks
  • Track state per connection
  • Implement connection priority
  • Coordinate reconnection attempts

Performance Impact

Event monitoring:

  • Minimal overhead
  • Efficient event notification
  • No polling required
  • Immediate event detection

Logging

Log socket events for debugging:

  • Connection attempts
  • Success/failure events
  • Error messages
  • Timeout conditions

Use Add to Event Log for persistent logging.

Best Practices

Socket event handling best practices:

  1. Always handle all event types
  2. Implement exponential backoff for retries
  3. Log events for debugging
  4. Set reasonable timeout values
  5. Clean up on disconnect
  6. Test failover scenarios

Integration with Features

Socket events integrate with:

Troubleshooting

Debug socket connection issues:

  • Frequent disconnects: Check network stability
  • Connection never succeeds: Verify host and port
  • Timeout errors: Increase timeout or check network
  • TLS errors: Check certificates and system time

See Also