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

Website hangs when trying to connect to an offline relay #2

Open
chakany opened this issue Feb 10, 2025 · 2 comments
Open

Website hangs when trying to connect to an offline relay #2

chakany opened this issue Feb 10, 2025 · 2 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@chakany
Copy link
Member

chakany commented Feb 10, 2025

Incident

I attempted to mitigate slowdowns caused by connecting to multiple relays simultaneously and fetching all their events by making the kitchen relay the default relay. However, when the kitchen.zap.cooking relay went down, the website stopped functioning. I promptly added another default relay, b6031c7, but it was ineffective. While the profiles loaded and you could view those recipes, nothing on the recents page or any of the categories loaded, but the profiles did. Notably, if I removed the offline relay, the website would suddenly start working again.

Initial Assumptions

My initial assumption is that there is a bug in the NDK that is causing this issue. I will open an issue to report it.

Mitigation

For now, I have implemented a temporary fix by filtering out the kitchen.zap.cooking relay from the relay list on load in the commit 710dab3.

@chakany chakany added bug Something isn't working help wanted Extra attention is needed labels Feb 10, 2025
@chakany chakany self-assigned this Feb 10, 2025
@chakany
Copy link
Member Author

chakany commented Feb 10, 2025

I ran a test. Using the below sample code, the following behavior is observed.

In the one on top, there is no connection to kitchen, and events work just fine! However on the bottom when trying to connect to kitchen, it hangs, and eventually spits out an error message.

This behavior is only observed when using the fetchEvents function over subscribe.

Image

Can't fetch when one relay is offline:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nostr Event Viewer</title>
</head>
<body>
    <h1>Nostr Event Viewer</h1>
    <div id="events"></div>

    <script type="module">
        import NDK from 'https://esm.sh/@nostr-dev-kit/[email protected]';


        const nostr = new NDK({
            explicitRelayUrls: [
                "wss://relay.nostr.band",
                "wss://relay.chakany.systems"
            ],
        });

        (async () => {
            try {
                await nostr.connect();
                console.log("connected");

                const events = nostr.fetchEvents({ kinds: [1] });

                events.then((fetchedEvents) => {
                  fetchedEvents.forEach((event) => {
                    const eventDiv = document.createElement('div');
                    eventDiv.textContent = event.content;
                    document.getElementById('events').appendChild(eventDiv);
                  });
                });
            } catch (error) {
                console.error(error);
            }
        })();
    </script>
</body>
</html>

Can fetch when one relay is offline:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nostr Event Viewer</title>
</head>
<body>
    <h1>Nostr Event Viewer</h1>
    <div id="events"></div>

    <script type="module">
        import NDK from 'https://esm.sh/@nostr-dev-kit/[email protected]';


        const nostr = new NDK({
            explicitRelayUrls: [
                "wss://relay.nostr.band",
                "wss://kitchen.zap.cooking"
            ],
        });

        (async () => {
            try {
                await nostr.connect();
                console.log("connected");
                const sub = nostr.subscribe({ kinds: [1] });
                sub.on("event", (event) => {
                    const eventDiv = document.createElement('div');
                    eventDiv.textContent = event.content;
                    document.getElementById('events').appendChild(eventDiv);
                });
            } catch (error) {
                console.error(error);
            }
        })();
    </script>
</body>
</html>

@chakany
Copy link
Member Author

chakany commented Feb 11, 2025

opened a bug report in ndk under nostr-dev-kit/ndk#306

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant