disconnect method

Future<void> disconnect()

Disconnect from the server temporarily. This disconnects but allows reconnecting later.

Use this when you want to temporarily disable realtime (e.g., when app goes to background, user logs out temporarily, or to save battery).

Subscriptions are preserved and will be re-registered when you call connect() again.

Example:

// Disconnect when app goes to background
await transmit.disconnect();

// Reconnect when app comes to foreground
await transmit.connect();

Implementation

Future<void> disconnect() async {
  if (_isClosed) {
    return;
  }

  _isManuallyDisconnected = true;

  // Stop heartbeat monitoring
  _stopHeartbeatMonitoring();

  // Cancel any pending reconnect attempts
  _reconnectTimer?.cancel();
  _reconnectTimer = null;
  _isReconnecting = false;
  _nextRetryTime = null;

  // Clean up current connection
  await _cleanupConnection();

  // Update status
  if (_status != TransmitStatus.disconnected) {
    _changeStatus(TransmitStatus.disconnected);
  }
}