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 URLfilters- Array of Nostr filters. Each filter is its own subscription, whichsocketPolicyMultiplexmay send together with otherssignal?- AbortSignal for cancellationpriority?- Send priority on the socket's queuemultiplex?- Passfalseto send each subscription as it was asked for, rather than lettingsocketPolicyMultiplexmerge it with others. Defaults totruetracker?- ATrackerfor deduplicationcontext?- Adapter contextautoClose?- Close the subscription after EOSE, on socket disconnect, or at the timeout, whichever comes firsttimeout?- 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 withautoClose, and to none withoutresubscribeAttempts?- 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 ofRelayReasonPrefix) are never retried, and each retry reaches back to the time of the refusal. Only meaningful withoutautoClose.isEventValid?- Signature check, defaulting toverifyEvent- Callback functions:
onEvent,onDeleted,onInvalid,onFiltered,onDuplicate,onDisconnect,onEose,onClosed,onCloseonClosed?: (message: string, url: string) => void- Called when the relay sends a CLOSED message, receiving the close reason and relay URLonClose?: () => 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 URLsfilters- Array of Nostr filtersthreshold?- 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,
})