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 = false,
  8. bool encrypt = false,
})

Send a message (auto-routes via S&F if recipient offline)

Implementation

Future<SendResult> sendMessage({
  required PeerId recipient,
  required Uint8List payload,
  String? folderPath,  // Target folder (default: 'inbox')
  MessagePriority priority = MessagePriority.normal,
  Duration? expiry,
  bool persistent = false,  // Keep after reading
  bool tryDirectFirst = false, // Set to true to try direct delivery first
  bool encrypt = false, // Seal the payload for the recipient (needs an encryptor)
}) async {
  _logger.fine('Sending message to ${recipient.toString().substring(0, 12)}... (folder: ${folderPath ?? 'inbox'})');

  // Sealing binds the ciphertext to the message id, so the id is minted
  // here rather than at submission.
  String? messageId;
  var flags = SFMessageFlags.none;
  if (encrypt) {
    final enc = encryptor;
    if (enc == null) {
      throw StateError('encrypt: true needs an SFClient built with a PayloadEncryptor');
    }
    messageId = _uuid.v4();
    payload = await enc.encryptBound(
      payload,
      PayloadBinding.forMessage(recipientPeerId: recipient, folderPath: folderPath, messageId: messageId),
      recipient,
    );
    flags = flags.withFlag(SFMessageFlags.encrypted);
  }

  final result = await _messageSender.sendMessage(
    recipient: recipient,
    payload: payload,
    folderPath: folderPath,
    priority: priority,
    expiry: expiry,
    persistent: persistent,
    tryDirectFirst: tryDirectFirst,
    messageId: messageId,
    flags: flags,
  );

  _logger.info('Send result: $result');
  return result;
}