Skip to main content

On MQTT Connected

This block is triggered when the MQTT connection status changes. Use it to handle connection and disconnection events.

On MQTT Connected Block

Overview

The On MQTT Connected block detects changes in MQTT broker connection status, allowing your application to respond to network connectivity changes.

Trigger Events

This block triggers on:

  • Connected: Successfully connected to MQTT broker
  • Disconnected: Connection to broker lost

Configuration

  • Event Type: Which event to trigger on (connected/disconnected/both)

Use Cases

Common connection handling scenarios:

On Connected

  • Publish device online status
  • Resume data transmission
  • Synchronize state with broker

On Disconnected

  • Log disconnection event
  • Queue messages for later
  • Alert monitoring systems
  • Activate local-only mode

Reconnection Handling

MQTT feature automatically handles reconnection:

  • Exponential backoff between attempts
  • Configurable retry limits
  • Automatic topic resubscription
  • Retained message reception

Use this block to:

  • Track reconnection events
  • Alert on persistent failures

Application Flow

Typical connection handling flow:

  1. Device boots, attempts MQTT connection
  2. On Connected triggers when successful
  3. Application subscribes to topics
  4. Application publishes online status
  5. If connection lost, On Disconnected triggers
  6. Application queues outgoing messages
  7. Automatic reconnection attempts
  8. On Reconnected triggers when restored

Status Monitoring

Monitor connection health:

  • Log connection/disconnection events
  • Track uptime statistics
  • Calculate connection reliability
  • Alert on frequent disconnections

Network Dependency

MQTT connection depends on:

Monitor network status alongside MQTT status for complete picture.

Error Recovery

Handle connection errors:

  • Network unavailable: Wait for network restoration
  • Authentication failed: Check credentials

Message Queuing

When disconnected:

  • Queue important messages locally
  • Transmit when connection restored
  • Implement queue size limits
  • Handle queue overflow gracefully

Best Practices

Connection management best practices:

  • Always handle both connected and disconnected events
  • Don't publish immediately on connect - wait briefly
  • Log all connection events for debugging
  • Set realistic timeout values

Troubleshooting

Common connection issues:

  • Frequent disconnects: Check network stability, adjust keep-alive
  • Cannot connect: Verify broker address, port, and credentials
  • Connects but drops: Check firewall rules, keep-alive settings
  • Never reconnects: Check reconnection settings and network

See Also