subscribeToGroup method

Future<void> subscribeToGroup(
  1. String groupName
)

Subscribes the local node to a discovered group.

This method:

  1. Validates that adding this group won't exceed the 131-byte limit.
  2. Updates the database.
  3. Tells the background service to stop advertising.
  4. Recalculates the endpoint name with ALL subscribed groups.
  5. 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
}