sendEventToRelaysAsync method

  1. @override
Future<NostrEventOkCommand> sendEventToRelaysAsync(
  1. NostrEvent event, {
  2. required Duration timeout,
})
override

This method is responsible for sending an event to all relays that you did registered with the init method, and gets you a Future of NostrEventOkCommand that will be triggered when the event is accepted by the relays.

Implementation

@override
Future<NostrEventOkCommand> sendEventToRelaysAsync(
  NostrEvent event, {
  required Duration timeout,
}) {
  var isSomeOkTriggered = false;

  final completers = <Completer<NostrEventOkCommand>>[];

  _runFunctionOverRelationIteration((relay) {
    final completer = Completer<NostrEventOkCommand>();
    completers.add(completer);

    Future.delayed(timeout, () {
      if (!isSomeOkTriggered) {
        throw TimeoutException(
          'the event with id: ${event.id} has timed out after: ${timeout.inSeconds} seconds',
        );
      }
    });

    final serialized = event.serialized();


 if(event.id == null) {
    throw Exception('event id cannot be null');
  }


    _registerOnOklCallBack(
      associatedEventId: event.id!,
      relay: relay.url,
      onOk: (relay, ok) {
        isSomeOkTriggered = true;
        completer.complete(ok);
      },
    );

    relay.socket.sink.add(serialized);
    utils.log(
      'event with id: ${event.id} is sent to relay with url: ${relay.url}',
    );
  });

  return Future.any(completers.map((e) => e.future));
}