startEventsSubscription method

  1. @override
NostrEventsStream startEventsSubscription({
  1. required NostrRequest request,
  2. void onEose(
    1. String relay,
    2. NostrRequestEoseCommand ease
    )?,
  3. bool useConsistentSubscriptionIdBasedOnRequestData = false,
  4. List<String>? relays,
})
override

This method will send a request to all relays that you did registered with the init method, and gets your a Stream of NostrEvents that will be filtered by the request's subscriptionId automatically.

if the you do not specify a subscriptionId in the request, it will be generated automatically from the library. (This is recommended only of you're not planning to use the closeEventsSubscription method.

example:

Nostr.instance.relays.startEventsSubscription(request);

Implementation

@override
NostrEventsStream startEventsSubscription({
  required NostrRequest request,
  void Function(String relay, NostrRequestEoseCommand ease)? onEose,
  bool useConsistentSubscriptionIdBasedOnRequestData = false,
  List<String>? relays,
}) {
  final serialized = request.serialized(
    subscriptionId: useConsistentSubscriptionIdBasedOnRequestData
        ? null
        : Nostr.instance.utilsService.random64HexChars(),
  );

  _registerNewRelays(relays ?? relaysList!).then((_) {
    _runFunctionOverRelationIteration((relay) {
      final relayUrl = relay.url;

      if (relays?.containsRelay(relayUrl) ?? true) {
        _registerOnEoselCallBack(
          subscriptionId: request.subscriptionId!,
          onEose: onEose ?? (relay, eose) {},
          relay: relayUrl,
        );

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

  final requestSubId = request.subscriptionId;
  final subStream = streamsController.events.where(
    (event) => _filterNostrEventsWithId(event, requestSubId),
  );

  return NostrEventsStream(
    request: request,
    stream: subStream,
    subscriptionId: request.subscriptionId!,
  );
}