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

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:
- Detect error event
- Log error details
- Wait before retry (exponential backoff)
- Attempt reconnection
- 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
Related Blocks
- Socket Connect: Establish connection
- Socket Send: Send data when connected
- On Socket Data: Receive data
- Variable Set: Store event state
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:
- Always handle all event types
- Implement exponential backoff for retries
- Log events for debugging
- Set reasonable timeout values
- Clean up on disconnect
- 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