The NPool class is a NRelay implementation for connecting to multiple relays.
const pool = new NPool({ open: (url) => new NRelay1(url), reqRouter: async (filters) => new Map([ ['wss://relay1.mostr.pub', filters], ['wss://relay2.mostr.pub', filters], ]), eventRouter: async (event) => ['wss://relay1.mostr.pub', 'wss://relay2.mostr.pub'], }); // Now you can use the pool like a regular relay. for await (const msg of pool.req([{ kinds: [1] }])) { if (msg[0] === 'EVENT') console.log(msg[2]); if (msg[0] === 'EOSE') break; }
This class is designed with the Outbox model in mind.
Instead of passing relay URLs into each method, you pass functions into the contructor that statically-analyze filters and events to determine which relays to use for requesting and publishing events.
If a relay wasn't already connected, it will be opened automatically.
Defining open will also let you use any relay implementation, such as NRelay1.
Note that pool.req may stream duplicate events, while pool.query will correctly process replaceable events and deletions within the event set before returning them.
pool.req will only emit an EOSE when all relays in its set have emitted an EOSE, and likewise for CLOSED.
[Symbol.asyncDispose](): Promise<void>
event(event: NostrEvent,opts?: { signal?: AbortSignal; relays?: string[]; },): Promise<void>
Events are sent to relays according to the eventRouter.
Returns a fulfilled promise if ANY relay accepted the event,
or a rejected promise if ALL relays rejected or failed to publish the event.
Returns a new pool instance that uses the given relays. Connections are shared with the original pool.
query(filters: NostrFilter[],opts?: { signal?: AbortSignal; relays?: string[]; },): Promise<NostrEvent[]>
This method calls .req internally and then post-processes the results.
Please read the definition of .req.
- The strategy is to seek regular events quickly, and to wait to find the latest versions of replaceable events.
- Filters for replaceable events will wait for all relays to
EOSE(orCLOSE, or for the signal to be aborted) to ensure the latest event versions are retrieved. - Filters for regular events will stop as soon as the filters are fulfilled.
- Events are deduplicated, sorted, and only the latest version of replaceable events is kept.
- If the signal is aborted, this method will return partial results instead of throwing.
To implement a custom strategy, call .req directly.
req(filters: NostrFilter[],opts?: { signal?: AbortSignal; relays?: string[]; },): AsyncIterable<NostrRelayEVENT
| NostrRelayEOSE
| NostrRelayCLOSED>
Sends a REQ to relays based on the configured reqRouter.
EVENT messages from the selected relays are yielded.
EOSE and CLOSE messages are only yielded when all relays have emitted them.
Deduplication of EVENT messages is attempted, so that each event is only yielded once.
A circular set of 1000 is used to track seen event IDs, so it's possible that very
long-running subscriptions (with over 1000 results) may yield duplicate events.