Relay Pool ​
The NPool
class is an NRelay
implementation for connecting to multiple relays.
Usage ​
This class is designed with the Outbox model in mind.
Instead of passing relay URLs into each method, you pass functions into the constructor that statically-analyzes filters and events to determine which relays to use for requesting and publishing events.
const pool = new NPool({
open(url) {
return new NRelay1(url);
},
async reqRelays(filters) {
return [/* Return an array of relay URLs. */];
},
async eventRelays(event) {
return [/* Return an array of relay URLs. */];
},
});
// 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;
}
INFO
- If a relay wasn't already connected, it will be opened automatically. Defining
open
will also let you use any relay implementation, such asNRelay1
. pool.req
may stream duplicate events, whilepool.query
will correctly process replaceable events and deletions within the event set before returning them.pool.req
will only emit anEOSE
when all relays in its set have emitted anEOSE
, and likewise forCLOSED
.
Options ​
open
- A function like(url: string) => NRelay
. This function should return a new instance ofNRelay
for the given URL.reqRelays
- A function like(filters: NostrFilter[]) => Promise<string[]>
. This function should return an array of relay URLs to use for making a REQ to the given filters. To support the Outbox model, it should analyze theauthors
field of the filters.eventRelays
- A function like(event: NostrEvent) => Promise<string[]>
. This function should return an array of relay URLs to use for publishing an EVENT. To support the Outbox model, it should analyze thepubkey
field of the event.
TIP
The url
parameter is a unique relay identifier (string), and doesn't technically have to be a URL, as long as you handle it correctly in your open
function.