sendEventToRelaysAsync method

  1. @override
Future<NostrEventOkCommand> sendEventToRelaysAsync(
  1. NostrEvent event, {
  2. required Duration timeout,
  3. void onOk(
    1. NostrEventOkCommand ok
    )?,
})
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,
  void Function(NostrEventOkCommand ok)? onOk,
}) {
  final completer = Completer<NostrEventOkCommand>();
  var okTriggered = false;

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

  final serialized = event.serialized();

  _registerOnOklCallBack(
    event.id,
    (ok) {
      okTriggered = true;
      completer.complete(ok);
    },
  );

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

  return completer.future;
}