subscribeToGroup method
Subscribes the local node to a discovered group.
This method:
- Validates that adding this group won't exceed the 131-byte limit.
- Updates the database.
- Tells the background service to stop advertising.
- Recalculates the endpoint name with ALL subscribed groups.
- Restarts advertising with the new combined string.
Throws GroupSubscriptionLimitException if the combined endpoint name would exceed the 131-byte limit.
Example combined endpoint name:
AP|1|R|protest-2026,aid-sector-5|a1b2c3d4
Implementation
Future<void> subscribeToGroup(String groupName) async {
_log('Subscribing to group: "$groupName"');
// Get current subscriptions
final currentGroupIds = await _getSubscribedGroupIds();
// Already subscribed?
if (currentGroupIds.contains(groupName)) {
_log('Already subscribed to "$groupName"');
return;
}
// Validate byte limit BEFORE committing
_validateEndpointLength([...currentGroupIds, groupName]);
// Commit the subscription to the DB
await _db.subscribeToGroup(groupName, exclusive: false);
// Recalculate and re-advertise with ALL subscribed groups
final updatedGroupIds = await _getSubscribedGroupIds();
updateAdvertising(
groupIds: updatedGroupIds,
); // Fire and forget to avoid UI lag
}