sendMessage method

Future<SendResult> sendMessage({
  1. required PeerId recipient,
  2. required Uint8List payload,
  3. String? folderPath,
  4. MessagePriority priority = MessagePriority.normal,
  5. Duration? expiry,
  6. bool persistent = false,
  7. bool tryDirectFirst = true,
  8. String? messageId,
  9. SFMessageFlags flags = SFMessageFlags.none,
})

Send message with automatic retry and failover

Implementation

Future<SendResult> sendMessage({
  required PeerId recipient,
  required Uint8List payload,
  String? folderPath,  // Target folder
  MessagePriority priority = MessagePriority.normal,
  Duration? expiry,
  bool persistent = false,  // Keep after reading
  bool tryDirectFirst = true,
  String? messageId,
  SFMessageFlags flags = SFMessageFlags.none,
}) async {
  _sendAttempts++;

  // Try direct delivery first if requested
  if (tryDirectFirst) {
    final directResult = await sendDirect(
      recipient,
      payload,
      priority,
      expiry,
      folderPath: folderPath,
      persistent: persistent,
    );
    if (directResult.success) {
      _successfulSends++;
      _directDeliveries++;
      return directResult;
    }
  }

  // Fallback to S&F server storage
  final sfResult = await _sendViaServerWithRetry(
    recipient,
    payload,
    priority,
    expiry,
    folderPath: folderPath,
    persistent: persistent,
    messageId: messageId,
    flags: flags,
  );

  if (sfResult.success) {
    _successfulSends++;
    _storedMessages++;
  } else {
    _failedSends++;
  }

  return sfResult;
}