sendMessage method
Saves a new outgoing message to the local database as PENDING.
This method does NOT perform any network I/O. The message will be picked up by the background AirpassSyncEngine during the next connect-sync-drop cycle.
Small-message optimization: If the payload + metadata is under kMicroMessageMaxBytes (100 bytes), the sync engine may broadcast it connectionlessly via the BLE endpoint name — no Wi-Fi Direct handshake required.
payload — the message content (text or encoded data).
targetId — the target identifier:
- A group ID (e.g., 'protest-2026') for group messages
- A node UUID for direct messages
- '*' for broadcast to all nodes
Returns the generated message ID.
Implementation
Future<String> sendMessage({
required String payload,
required String targetId,
}) async {
final messageId = _uuid.v4();
final payloadBytes = utf8.encode(payload);
await _db.createMessage(
messageId: messageId,
senderId: _localNodeId,
targetId: targetId,
payload: payloadBytes,
ttl: kAirpassDefaultMaxHops,
);
_log(
'Saved outgoing message $messageId for target "$targetId" (${payloadBytes.length} bytes)',
);
// Wake up the background service's scan loop to immediately broadcast the message
triggerImmediateSync();
return messageId;
}