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

lifecycle is broken for non explicit relays #303

Open
riccardobl opened this issue Jan 30, 2025 · 0 comments
Open

lifecycle is broken for non explicit relays #303

riccardobl opened this issue Jan 30, 2025 · 0 comments

Comments

@riccardobl
Copy link

riccardobl commented Jan 30, 2025

I've encountered several inexplicable issues when using NDK in my code and determined that they were caused by a broken connection lifecycle, that disconnects relays even if subscriptions to them are still active. Unless they are set in explicitRelayUrls .

I wrote a simple test case.
While the issue likely affects every platform, i suggest to try to run the test in a webpage in firefox, since it is easier to see it in this way.

Test that works as expected

Open the dev console, go into the network tab and look at the websocket events, you'll see the websocket stays connected forever

function test() {
    const relays = ['wss://relay.snort.social'];
    const ndk = new NDK({
        explicitRelayUrls: relays
    });
    const sub = ndk.subscribe({
        since: Math.floor(Date.now() / 1000)
    },undefined,NDKRelaySet.fromRelayUrls(relays,ndk), false);
    sub.on('event', (event) => {
        console.log('Event:', event);
    });
    sub.start().catch(console.error);
}

test();

Test with the issue

this is the same as the previous code, minus explicitRelayUrls.
Open the dev console, go into the network tab and look at the websocket events. You'll see that after a while the websocket disconnects with connection closed 1005 :
Image

function test() {
    const relays = ['wss://relay.snort.social'];
    const ndk = new NDK();
    const sub = ndk.subscribe({
        since: Math.floor(Date.now() / 1000)
    },undefined,NDKRelaySet.fromRelayUrls(relays,ndk), false);
    sub.on('event', (event) => {
        console.log('Event:', event);
    });
    sub.start().catch(console.error);
}

test();

This is a very insidious bug, since ndk will reconnect as soon as an event is sent by the client.
In an application that exchanges events frequently with the relay, this issue could be mistaken with rate limiting, or a bad relay.

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

1 participant