Skip to main content
Home

latest

Framework for Nostr on Deno and web. 🛸

This package works with Deno, BrowsersIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
This package works with Browsers
JSR Score
94%
Published
2 months ago (0.46.5)
class NPool
implements NRelay

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.

Constructors

new
NPool(opts: NPoolOpts<T>)

Type Parameters

T extends NRelay = NRelay

Properties

readonly
relays: ReadonlyMap<string, T>
private
_relays: Map<string, T>

Methods

close(): Promise<void>

Close all the relays in the pool.

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.

group(urls: string[]): NPool<T>

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 (or CLOSE, 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.

Get or create a relay instance for the given URL.

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.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@nostrify/nostrify

Import symbol

import { NPool } from "@nostrify/nostrify";
or

Import directly with a jsr specifier

import { NPool } from "jsr:@nostrify/nostrify";

Add Package

pnpm i jsr:@nostrify/nostrify
or (using pnpm 10.8 or older)
pnpm dlx jsr add @nostrify/nostrify

Import symbol

import { NPool } from "@nostrify/nostrify";

Add Package

yarn add jsr:@nostrify/nostrify
or (using Yarn 4.8 or older)
yarn dlx jsr add @nostrify/nostrify

Import symbol

import { NPool } from "@nostrify/nostrify";

Add Package

vlt install jsr:@nostrify/nostrify

Import symbol

import { NPool } from "@nostrify/nostrify";

Add Package

npx jsr add @nostrify/nostrify

Import symbol

import { NPool } from "@nostrify/nostrify";

Add Package

bunx jsr add @nostrify/nostrify

Import symbol

import { NPool } from "@nostrify/nostrify";