startEventsSubscription method

  1. @override
NostrEventsStream startEventsSubscription({
  1. required NostrRequest request,
  2. void onEose(
    1. NostrRequestEoseCommand ease
    )?,
  3. bool useConsistentSubscriptionIdBasedOnRequestData = false,
})
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(NostrRequestEoseCommand ease)? onEose,
  bool useConsistentSubscriptionIdBasedOnRequestData = false,
}) {
  final serialized = request.serialized(
    subscriptionId: useConsistentSubscriptionIdBasedOnRequestData
        ? null
        : Nostr.instance.utilsService.random64HexChars(),
  );

  _registerOnEoselCallBack(request.subscriptionId!, onEose);

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

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

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