Skip to content

Instantly share code, notes, and snippets.

@kuuote
Created December 5, 2025 09:05
Show Gist options
  • Select an option

  • Save kuuote/a2403aa49c6960e72a9dba555cf61923 to your computer and use it in GitHub Desktop.

Select an option

Save kuuote/a2403aa49c6960e72a9dba555cf61923 to your computer and use it in GitHub Desktop.
main.mtsにNode.js向けの以下のコードを出力してください
- ./user というファイルから文字列を読み込みtrimして格納
- rx-nostrを使用しuserの中身をpubkeyとしてkind:3を取得、そのeventのtagのうち["p", {pubkey}]の形の物のpubkey部分を収集しfollowsという変数に格納

rx-nostrの使い方

createRxNostr

"rx-nostr"よりcreateRxNostrを、@rx-nostr/cryptoよりverifierをimportして以下のコードを書けば初期化できる

const rxNostr = createRxNostr({
  eoseTimeout: 1000,
  verifier,
});

relay

rxNostr.setDefaultRelaysに文字列の配列を渡すとリレー一覧を設定できる

req

"rx-nostr"よりcreateRxOneshotReqをimportして以下のコードを書けば単発でリクエストを送れる

import { createRxOneshotReq } from "rx-nostr";

// 単発で最新のテキストノートを5件だけ取得する例
const rxReq = createRxOneshotReq({
  filters: {
    kinds: [1],
    limit: 5,
  },
});

rxNostr.use(rxReq).subscribe(({ event }) => {
  if (event.kind === 1) {
    console.log(event.content);
  }
});
import { readFile } from "fs/promises";
import { createRxNostr, createRxOneshotReq } from "rx-nostr";
import { verifier } from "@rx-nostr/crypto";
async function main() {
const userPath = new URL("./user", import.meta.url);
const userPubkey = (await readFile(userPath, "utf8")).trim();
const rxNostr = createRxNostr({
eoseTimeout: 1000,
verifier,
});
rxNostr.setDefaultRelays([
"wss://relay.damus.io",
"wss://nos.lol",
"wss://relay.snort.social",
]);
const rxReq = createRxOneshotReq({
filters: {
kinds: [3],
authors: [userPubkey],
limit: 1,
},
});
const follows = new Set<string>();
await new Promise<void>((resolve, reject) => {
rxNostr.use(rxReq).subscribe({
next: ({ event }) => {
if (event.kind === 3 && Array.isArray(event.tags)) {
for (const tag of event.tags) {
if (Array.isArray(tag) && tag[0] === "p" && typeof tag[1] === "string") {
follows.add(tag[1]);
}
}
}
},
error: reject,
complete: resolve,
});
});
console.log([...follows]);
}
void main();
{
"dependencies": {
"@rx-nostr/crypto": "^0.0.0",
"rx-nostr": "^3.6.1",
"rxjs": "^7.8.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment