Skip to content

Request ​

Functions for requesting events from relays, with validation and deduplication.

Functions ​

requestOne(options) ​

Requests events from a single relay using the given filters. Returns a promise that resolves with deduplicated events once every subscription has closed.

Options:

  • relay - Relay URL
  • filters - Array of Nostr filters. Each filter is its own subscription, which socketPolicyMultiplex may send together with others
  • signal? - AbortSignal for cancellation
  • priority? - Send priority on the socket's queue
  • multiplex? - Pass false to send each subscription as it was asked for, rather than letting socketPolicyMultiplex merge it with others. Defaults to true
  • tracker? - A Tracker for deduplication
  • context? - Adapter context
  • autoClose? - Close the subscription after EOSE, on socket disconnect, or at the timeout, whichever comes first
  • timeout? - Close the subscription this many milliseconds after the relay is sent it, so time spent waiting in the socket's queue doesn't count. A request that is never sent, as on a socket that never opens, closes this long after it was made. Defaults to 8 seconds with autoClose, and to none without
  • resubscribeAttempts? - How many times to ask again for a subscription the relay dropped for a reason that might not hold next time, such as a rate limit. Defaults to 0. Refusals NIP-01 marks as final (restricted:, blocked:, and the rest of RelayReasonPrefix) are never retried, and each retry reaches back to the time of the refusal. Only meaningful without autoClose.
  • isEventValid? - Signature check, defaulting to verifyEvent
  • Callback functions: onEvent, onDeleted, onInvalid, onFiltered, onDuplicate, onDisconnect, onEose, onClosed, onClose
    • onClosed?: (message: string, url: string) => void - Called when the relay sends a CLOSED message, receiving the close reason and relay URL
    • onClose?: () => void - Called when the subscription is fully closed

request(options) ​

Requests events from multiple relays in parallel, sharing one tracker across them. Returns a promise that resolves with all events from all relays.

Options:

  • relays - Array of relay URLs
  • filters - Array of Nostr filters
  • threshold? - Fraction of relays that must close before completing, after which the rest are closed (default: 1)
  • All other options from requestOne

An auto-closing request for a single filter of ids resolves as soon as it has every id.

load(options) ​

Calls request with autoClose: true, for a one-shot request. It takes the same options apart from autoClose.

Example ​

typescript
import {load, request} from "@welshman/net"

// Without autoClose or a signal this streams indefinitely, and the default socket
// policies re-send the subscription when the socket reconnects
const events = await request({
  relays: ["wss://relay1.com", "wss://relay2.com"],
  filters: [{kinds: [1], limit: 10}],
  context,
  onEvent: (event, url) => console.log(`Event from ${url}:`, event.id)
})

// A load resolves once every relay has eosed or timed out
const profileEvents = await load({
  relays: ["wss://relay1.com"],
  filters: [{kinds: [0], authors: ["pubkey1", "pubkey2"]}],
  timeout: 3000,
  context,
})