Class NDKSubscription

Represents a subscription to an NDK event stream.

Emits

event Emitted when an event is received by the subscription.

  • ({NDKEvent} event - The event received by the subscription,
  • {NDKRelay} relay - The relay that received the event,
  • {NDKSubscription} subscription - The subscription that received the event.)

Emits

event:dup Emitted when a duplicate event is received by the subscription.

  • {NDKEvent} event - The duplicate event received by the subscription.
  • {NDKRelay} relay - The relay that received the event.
  • {number} timeSinceFirstSeen - The time elapsed since the first time the event was seen.
  • {NDKSubscription} subscription - The subscription that received the event.

Emits

eose - Emitted when all relays have reached the end of the event stream.

  • {NDKSubscription} subscription - The subscription that received EOSE.

Emits

close - Emitted when the subscription is closed.

  • {NDKSubscription} subscription - The subscription that was closed.

Example

const sub = ndk.subscribe({ kinds: [1] }); // Get all kind:1s
sub.on("event", (event) => console.log(event.content); // Show the content
sub.on("eose", () => console.log("All relays have reached the end of the event stream"));
sub.on("close", () => console.log("Subscription closed"));
setTimeout(() => sub.stop(), 10000); // Stop the subscription after 10 seconds

Description

Subscriptions are created using NDK.subscribe.

Event validation

By defaults, subscriptions will validate events to comply with the minimal requirement of each known NIP. This can be disabled by setting the skipValidation option to true.

Example

const sub = ndk.subscribe({ kinds: [1] }, { skipValidation: false });
sub.on("event", (event) => console.log(event.content); // Only valid events will be received

Hierarchy

Constructors

Properties

closeOnEose: boolean

Whether the subscription should close when all relays have reached the end of the event stream.

debug: Debugger
eosesSeen: Set<NDKRelay> = ...

Relays that have sent an EOSE.

eventFirstSeen: Map<string, number> = ...

Events that have been seen by the subscription, with the time they were first seen.

eventsPerRelay: Map<NDKRelay, Set<string>> = ...

Events that have been seen by the subscription per relay.

filters: NDKFilter<NDKKind>[]
internalId: string
ndk: default
pool: NDKPool
relayFilters?: Map<string, NDKFilter<NDKKind>[]>

Tracks the filters as they are executed on each relay

relaySet?: NDKRelaySet
skipValidation: boolean = false
skipVerification: boolean = false
subId?: string

Accessors

Methods

  • Called when an event is received from a relay or the cache

    Parameters

    • event: NDKEvent
    • relay: undefined | NDKRelay
    • fromCache: boolean = false

      Whether the event was received from the cache

    • optimisticPublish: boolean = false

      Whether this event is coming from an optimistic publish

    Returns void

  • Start the subscription. This is the main method that should be called after creating a subscription.

    Returns Promise<void>

Generated using TypeDoc