onReceive method

  1. @override
void onReceive(
  1. Broadcast localityEvent
)

Processes broadcasts dispatched to the channel.

Implementation models should evaluate incoming data parameters using a matching block on the localityEvent parameter's internal structural fields to apply reactive updates.

Implementation

@override
void onReceive(Broadcast localityEvent) {
  switch (localityEvent.event) {
    case 'subscribe':
      final sub = TopicSubscription.fromMap(localityEvent.getPayload());
      subscriptions[sub.topic] = sub;
      notifyListeners();
      break;
    case 'unsubscribe':
      final payload = localityEvent.getPayload();
      final topic = payload['topic'] as String?;
      if (topic != null) {
        subscriptions.remove(topic);
        notifyListeners();
      }
      break;
  }
}