Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nostr connection times out in Docker environment #431

Open
gaboe opened this issue Sep 16, 2024 · 2 comments
Open

Nostr connection times out in Docker environment #431

gaboe opened this issue Sep 16, 2024 · 2 comments

Comments

@gaboe
Copy link

gaboe commented Sep 16, 2024

Description

When running our application in a Docker container, we're experiencing connection timeouts when trying to connect to Nostr relays. The same code works fine on macOS bun, but fails in bun the Docker environment.

Also i have tried using nodejs with polyfill hack, but it didn't work either #347

Is this package supossed to work on server?

import { defineEventHandler } from "h3";
import { SimplePool } from "nostr-tools";
import { nip19, nip04 } from "nostr-tools";
import { finalizeEvent, getPublicKey } from "nostr-tools/pure";

const RELAYS = [
  "wss://relay.damus.io",
  "wss://relay.primal.net",
  "wss://relay.0xchat.com",
  "wss://relay.siamstr.com",
];

const secretKey = nip19.decode("nsec1").data;

export default defineEventHandler(async () => {
  const pool = new SimplePool();
  const recipientPubkeys = ["npub"].map(
    (pubkey) => nip19.decode(pubkey).data as string
  );

  const content = "test";

  try {
    const relayConnections = await Promise.all(
      RELAYS.map((relay) => pool.ensureRelay(relay))
    );

    for (const recipientPubkey of recipientPubkeys) {
      const encryptedContent = await nip04.encrypt(
        secretKey,
        recipientPubkey,
        content
      );

      const eventTemplate = {
        kind: 4,
        created_at: Math.floor(Date.now() / 1000),
        tags: [["p", recipientPubkey]],
        content: encryptedContent,
        pubkey: getPublicKey(secretKey),
      };

      const signedEvent = finalizeEvent(eventTemplate, secretKey);

      const pubs = relayConnections.map((relay) => relay.publish(signedEvent));
      const failedRelays = [];
      await Promise.all(
        pubs.map(async (pub, i) => {
          try {
            await pub;
          } catch (err) {
            failedRelays.push(RELAYS[i]);
          }
        })
      );
      console.log("Published private message to Nostr!");

      if (failedRelays.length > 0) {
        throw new Error("Failed relays: " + failedRelays.join(", "));
      }

      return {
        success: true,
        message: "Form submitted and private notification sent",
      };
    }
  } catch (error) {
    console.error(
      "Error sending private message to Nostr:",
      error,
      JSON.stringify(error),
      error instanceof Error ? "Failed relays: " + error.message : ""
    );
    return {
      success: false,
      message: "Form submitted but private notification failed",
    };
  } finally {
    await pool.close(RELAYS);
  }
});

@gaboe
Copy link
Author

gaboe commented Sep 16, 2024

I have added whole minimal reproducible repo https://github.com/gaboe/nostr-api

I have been strugling wih this for few hours, i would appreciate any help.

@antonioconselheiro
Copy link
Contributor

antonioconselheiro commented Sep 20, 2024

Did you tried to use useWebSocketImplementation from https://github.com/nbd-wtf/nostr-tools?tab=readme-ov-file#interacting-with-multiple-relays?

I mean, like this config:

import { useWebSocketImplementation } from 'nostr-tools/pool'

useWebSocketImplementation(require('ws'))

I think you can try this too:

Object.assign(global, { WebSocket: require('ws') });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants