listen method

Future<void> listen({
  1. int retryDelaySeconds = 5,
})

Starts listening for notifications from the Haveno service.

If the service is not connected, it will retry every minute until successful. retryDelaySeconds specifies the delay (in seconds) between retries after errors.

Implementation

Future<void> listen({int retryDelaySeconds = 5}) async {
  // Prevent multiple listeners from starting.
  if (_isListening) return;

  _isListening = true;

  // Keep retrying until the daemon is connected.
  while (!havenoChannel.isConnected) {
    print('Service is not connected, retrying in 1 minute...');
    await Future.delayed(const Duration(minutes: 1));
  }

  try {
    // Register and listen to notifications from the service.
    ResponseStream<NotificationMessage> responseStream = havenoChannel.notificationsClient!
        .registerNotificationListener(RegisterNotificationListenerRequest());

    _notificationSubscription = responseStream.listen(
      (notification) => _handleNotification(notification),
      onError: (error) {
        print('Error receiving notifications: $error');
        _retryListen(retryDelaySeconds);
      },
      onDone: () {
        print('Notification stream closed');
        _retryListen(retryDelaySeconds);
      },
      cancelOnError: true,
    );
  } catch (e) {
    print('Failed to start notification listener: $e');
    _retryListen(retryDelaySeconds);
  }
}